UITableView
属性:初始化方法
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style: UITableViewStylePlain];
tableView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:tableView];
[tableView release];
设置行高
tableView.rowHeight = 100;tableView的协议有两套<UITableViewDataSource, UITableViewDelegate>两个协议需要设置两个代理人,还有一套协议是UIScrollerView的协议
// 设置代理人
tableView.dataSource = self;
// 设置代理人
tableView.delegate = self;
很重要的协议
dataSource的协议方法-(NSInteger)tableView:numberOfRowsInSection:
指定每个分区有多少行
// 标记方法, 通过标记快速找到方法
#pragma mark tableView必须实现的协议方法:指定每个分区有多少行!!!!!!!!!!!!!!!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// 可以根据分区控制行数
if (section % 2 == 0) {
return 10;
} else{
return self.arr.count;
}
// 一行显示一个人,用数组来显示
// 超过个数会崩溃(数组越界)
// return self.arr.count;
}
-(UITableViewCell *)tableView:cellForRowAtIndexPath:(NSIndexPath *)indexpath
通过这个方法可以让tableView显示内容:tableViewCell通过重用避免了多余的创建,一般来讲tableView显示的cell数是有限的,为了提高效率.避免重复创建,利用重用解决问题.重用也是tableView最常见的面试问题
1.先指定一个cell的重用标识:一般来说一个tableView对应一个重用标识.重用标识的作用就是告诉系统,那个cell对应的tableView
2.系统先会根据重用标识在重用池找,有没有闲置的cell,如果有直接拿来用,如果没有再创建
3.cell提供了三种视图textLable,detailTextLable,imageView三种视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// tableViewCell通过重用避免了多余的创建,一般来讲一个tableView显示的cell数是有限的,所以为了提高效率,避免重复的创建,利用重用解决问题,重用也是常见的tableView的面试问题!!!!!!!!!!!!!!!!! (解决滑动卡顿的问题)
// 1.先指定一个cell的重用标识
// 一般来讲,一个tableView对应一个重用标识,重用标识的作用就是告诉系统,哪个cell对应tableView
static NSString *reuse = @"resuse";
// 系统先会根据重用标识在重用池里找,有没有闲置的cell.如果有直接拿来用,如果没有,再创建
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
// 如果cell没找到,对应的cell是0x0
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
// cell提供了三种视图,两个lable,一个imageView
// 对应的行数
// cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
cell.textLabel.text = self.arr[indexPath.row];
// 分区
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
cell.imageView.image = [UIImage imageNamed:@"3.jpg"];
// 可以通过它找到指定的行数
// NSLog(@"%ld", indexPath.row);
return cell;
}
delegate-(void)tableView:didS.....
#pragma mark 这个是delegate的协议方法,主要功能就是实现点击!!!!!!!!!!!!!!!
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"row = %ld", indexPath.row);
// NSLog(@"section = %ld", indexPath.section);
// // 通过数组,index.row来打印点击的内容
// NSLog(@"%@", self.arr[indexPath.row]);
//
//}
以上是三个特别特别特别重要的协议方法
点击方法中还有一个协议是didDes.....的方法不要和点击方法弄混了(延迟一次点击)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"row = %ld", indexPath.row);
NSLog(@"section = %ld", indexPath.section);
// 通过数组,index.row来打印点击的内容
NSLog(@"%@", self.arr[indexPath.row]);
}
设置tableView中有多少分区
#pragma mark 设置tableView里有多少个分区(分区就是)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
设置分区的标题(header footer index)
#pragma mark 设置分区的标题
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// // 自带悬停效果
// return [NSString stringWithFormat:@"%ld", section];
//}
// 设置底部的标题
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
// return @"byebye";
//}
// 左边竖着的标题
//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// return @[@"A", @"B", @"C"];
//}
动态设置行高
// 动态设置行高
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.row % 2 == 0) {
// return 150;
// } else{
// return 100;
// }
//
//}
tableView包含两个部分还有scrollView的协议
573

被折叠的 条评论
为什么被折叠?



