上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法。
一、属性
1、frame:设置控件的尺寸和大小
2、backgroundColor:设置控件的颜色
3、style:获取表视图的样式
4、dataSource:设置UITableViewDataSource代理
5、delegate:设置UITableViewDelegate代理
6、backgroundView:设置背景视图
7、editing:是否允许编辑,默认为NO
8、sectionHeaderHeight:设置组表视图的头标签高度
9、sectionFooterHeight:设置组表视图的尾标签高度
10、allowsSelection:在非编辑下,行是否可以选中,默认YES
11、allowsSelectionDuringEditing:控制某一行时,是否可以编辑,默认NO
12、allowsMultipleSelection:是否可以选择多行,默认NO
13、allowsMutableSelectionDuringEditing:在选择多行的情况下,是否可以编辑,默认NO
14、sectionIndexTrackingBackgroundColor:设置选中部分的背景颜色
15、sectionIndexMinimumDisplayRowCount:显示某个组索引列表在右边当行数达到这个值,默认是NSInteger的最大值
16、sectionIndexColor:选择某个部分的某行改变这一行上文本的颜色
17、separatorStyle:设置单元格分隔线的样式
18、separatorColor:设置选中单元格分隔线的颜色
19、tableHeaderView:设置组表的头标签视图
20、tableFooterView:设置组表的尾标签视图
二、常见方法
1、- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
tableview 有多少个section
1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 2 3 return 5; 4 5 }
2、- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
对应的section有多少行
1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 2 3 return 10; 4 }
3、- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;——返回指定的 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;——返回指定的 section的header view 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;——返回指定的 section的footer view 的高度
1 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 2 return 60; 3 } 4 5 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 6 return 100; 7 } 8 9 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ 10 return 30; 11 }
4、- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
返回指定的row 的cell,可以返回自定义的cell;
这里只看看最基本的用法:
1 - (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 2 3 static NSString *Indentifier = @"UITableViewCell"; 4 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 5 if (!cell) { 6 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Indentifier]; 7 } 8 9 //移除所有子视图 10 [cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 11 UIView *view = (UIView*)obj; 12 [view removeFromSuperview]; 13 }]; 14 15 //添加新视图 16 UILabel *label = [[UILabel alloc]initWithFrame:(CGRect){70,20,200,20}]; 17 label.text = [NSString stringWithFormat:@"第%d行",(int)indexPath.row]; 18 [cell addSubview:label]; 19 20 UIImageView *imageIcon = [[UIImageView alloc] initWithFrame:(CGRect){10,5,50,50}]; 21 imageIcon.image = [UIImage imageNamed:@"u=3971024035,4095552302&fm=21&gp=0"]; 22 [cell addSubview:imageIcon]; 23 24 return cell; 25 26 }
如图:
5、- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回指定的section 的 header 的 title,如果这个section header 有返回view,那么title就不起作用了。
1 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 2 if (section==0) { 3 return @"标题一"; 4 }else 5 return @"标题二"; 6 7 }
如图:
6、 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
同理设置FooterView:- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
返回指定的 section header 的view,如果没有,这个函数可以不返回view
注意:设置了headerView一定不要忘记返回高度!
1 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 2 UIView *headerView = [[UIView alloc] initWithFrame:(CGRect){0,0,375,100}]; 3 4 UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){0,0,375,20}]; 5 title.text = @"headerView"; 6 title.textColor = [UIColor orangeColor]; 7 title.textAlignment = NSTextAlignmentCenter; 8 [headerView addSubview:title]; 9 10 UIImageView *headImage = [[UIImageView alloc] initWithFrame:(CGRect){10,20,355,80}]; 11 headImage.image =[UIImage imageNamed:@"u=1522827729,398642494&fm=21&gp=0"]; 12 [headerView addSubview:headImage]; 13 14 15 return headerView; 16 } 17 18 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 19 20 return 100; 21 }
如图:
7、 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableView.allowsSelection = YES 才行。
1 - (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 2 if (indexPath.section==0) { 3 switch (indexPath.row) { 4 case 0: 5 //点击每行想实现什么动作写在这里就好了 6 break; 7 8 default: 9 break; 10 } 11 } 12 }
下面附上完整代码:
1 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ 2 3 UITableView *tableView; 4 NSArray *dataArray; 5 6 } 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view, typically from a nib. 15 16 //初始化,设置位置大小 17 tableView = [[UITableView alloc] initWithFrame:(CGRect){0,20,self.view.frame.size.width,self.view.frame.size.height}]; 18 //添加到视图 19 [self.view addSubview:tableView]; 20 //设置UITableViewDeledate代理 21 tableView.delegate = self; 22 //设置UITableViewDataSource代理 23 tableView.dataSource = self; 24 //设置单元格分隔线样式 25 tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 26 27 tableView.allowsSelection = YES; 28 29 } 30 31 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 32 return 2; 33 } 34 35 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 36 37 return 10; 38 } 39 40 - (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 41 42 static NSString *Indentifier = @"UITableViewCell"; 43 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 44 if (!cell) { 45 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Indentifier]; 46 } 47 48 //移除所有子视图 49 [cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 50 UIView *view = (UIView*)obj; 51 [view removeFromSuperview]; 52 }]; 53 54 //添加新视图 55 UILabel *label = [[UILabel alloc]initWithFrame:(CGRect){70,20,200,20}]; 56 label.text = [NSString stringWithFormat:@"第%d行",(int)indexPath.row]; 57 [cell addSubview:label]; 58 59 UIImageView *imageIcon = [[UIImageView alloc] initWithFrame:(CGRect){10,5,50,50}]; 60 imageIcon.image = [UIImage imageNamed:@"u=3971024035,4095552302&fm=21&gp=0"]; 61 [cell addSubview:imageIcon]; 62 63 return cell; 64 65 } 66 67 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 68 return 60; 69 70 } 71 72 //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 73 // if (section==0) { 74 // return @"标题一"; 75 // }else 76 // return @"标题二"; 77 // 78 //} 79 80 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 81 UIView *headerView = [[UIView alloc] initWithFrame:(CGRect){0,0,375,100}]; 82 83 UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){0,0,375,20}]; 84 title.text = @"headerView"; 85 title.textColor = [UIColor orangeColor]; 86 title.textAlignment = NSTextAlignmentCenter; 87 [headerView addSubview:title]; 88 89 UIImageView *headImage = [[UIImageView alloc] initWithFrame:(CGRect){10,20,355,80}]; 90 headImage.image =[UIImage imageNamed:@"u=1522827729,398642494&fm=21&gp=0"]; 91 [headerView addSubview:headImage]; 92 93 94 return headerView; 95 } 96 97 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 98 99 return 100; 100 } 101 102 - (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 103 if (indexPath.section==0) { 104 switch (indexPath.row) { 105 case 0: 106 //点击每行想实现什么动作写在这里就好了 107 break; 108 109 default: 110 break; 111 } 112 } 113 }