UITableView的使用(UITableViewCell\section页眉\section页脚复用、sectionHeaderView点击效果)

来源:互联网 发布:淘宝售后在哪里查看 编辑:程序博客网 时间:2024/05/29 09:53

UITableView是列表视图,类似于QQAPP中联系人页面的功能。

UITableViewUIScrollView的子类,默认拥有UIScrollView的滚动功能,而不用设置contentSize

使用注意事项

1必须添加协议,以及实现代理方法

1-1 UITableViewDelegate   视图代理

1-2 UITableViewDataSource 数据源代理

初始化时,必须使用方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style

列表类型UITableViewStyle设置时,注意差异,如:

3-1 UITableViewStyleGrouped,如果有多个section时,当前section页眉不会在最顶端显示

3-2 UITableViewStylePlain,如果有多个section时,当前section页眉会在最顶端显示

4代理方法实现中,注意相同功能代理方法实现的冲突,如:

4-1 - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section不能同时设置

4-2 - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section不能同时设置

5、注意复用技术

5-1 UITableViewCell的复用

5-2 每个section中页眉视图的复用

5-3 每个section中页脚视图的复用


[objc] view plain copy
  1. @interface ViewController () <UITableViewDataSource, UITableViewDelegate>  
  2.   
  3. @property (nonatomicstrongUITableView *tableview;  
  4. @property (nonatomicstrongNSMutableArray *mainArray; // 数据源  
  5. @property (nonatomicstrongNSMutableArray *boolArray; // 用于控制section行数的数据源,即控制section的展开,或折叠  
  6.   
  7. @end  

[objc] view plain copy
  1. // 设置数据源  
  2. NSInteger sectionCount = arc4random() % 10 + 1// 随机数  
  3. sectionCount = 10;  
  4. self.mainArray = [[NSMutableArray alloc] init];  
  5. for (int i = 0; i < sectionCount; i++)  
  6. {  
  7.         NSInteger rowCount = arc4random() % 10 + 1// 随机数  
  8.         rowCount = 10;  
  9.         NSMutableArray *array = [NSMutableArray arrayWithCapacity:rowCount];  
  10.         for (int j = 0; j < rowCount; j++)  
  11.         {  
  12.             NSString *rowText = [NSString stringWithFormat:@"section(%@)-row(%@)", @(i + 1), @(j + 1)];  
  13.             [array addObject:rowText];  
  14.         }  
  15.         NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];  
  16.         [dict setObject:[NSString stringWithFormat:@"section(%@)", @(i + 1)] forKey:@"title"];  
  17.         [dict setObject:array forKey:@"array"];  
  18.           
  19.         [self.mainArray addObject:dict];  
  20. }  

[objc] view plain copy
  1. self.boolArray = [[NSMutableArray alloc] init];  
  2. for (int i = 0; i < sectionCount; i++)  
  3. {  
  4.         // 1为展开,0为折叠  
  5.         [self.boolArray addObject:@(1)];  
  6. }  


[objc] view plain copy
  1. self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];  
  2. [self.view addSubview:self.tableview];  
  3. self.tableview.backgroundColor = [UIColor redColor];  
  4.       
  5. /* 
  6. 设置代理 
  7. 1 设置实现代理方法的对象 
  8. 2 添加协议 
  9. 3 实现代理方法 
  10. */  
  11. self.tableview.delegate = self;  
  12. self.tableview.dataSource = self;  

[objc] view plain copy
  1. // 其他属性  
  2. // 头部视图  
  3. UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.00.0, CGRectGetWidth(self.view.bounds), 200.0)];  
  4. headerView.image = [UIImage imageNamed:@"headerImage"];  
  5. self.tableview.tableHeaderView = headerView;  
  6. // 尾部视图  
  7. UIImageView *footerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.00.0, CGRectGetWidth(self.view.bounds), 200.0)];  
  8. footerView.image = [UIImage imageNamed:@"footerImage"];  
  9. self.tableview.tableFooterView = footerView;  
  10. // 滚动回顶端,即点击状态栏视图时,自动滚动回顶端,默认YES,即打开  
  11. self.tableview.scrollsToTop = YES;  
  12. // UITableViewCell中分割线的颜色  
  13. self.tableview.separatorColor = [UIColor purpleColor];  
  14. // UITableViewCell中分割线样式,默认单条线,即UITableViewCellSeparatorStyleSingleLine  
  15. self.tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;  
  16. // UITableViewCell中分割线对齐,默认有间距(注意:与UITableViewCell的 separatorInset、layoutMargins 属性同时设置才有效)  
  17. self.tableview.separatorInset = UIEdgeInsetsMake(0.00.00.00.0);  
  18. self.tableview.layoutMargins = UIEdgeInsetsMake(0.00.00.00.0);  

[objc] view plain copy
  1. // UITableViewDataSource  
  2.   
  3. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  4. {  
  5.     // 总共有多少个section,默认是1个  
  6.       
  7.     NSInteger count = self.mainArray.count;  
  8.     return count;  
  9. }  
  10.   
  11. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  12. {  
  13.     // 每个section有多少个row  
  14.       
  15.     NSDictionary *dict = self.mainArray[section];  
  16.     NSArray *array = [dict objectForKey:@"array"];  
  17.     NSInteger count = 0;  
  18.     NSNumber *openNumber = self.boolArray[section];  
  19.     if (openNumber.intValue == 1)  
  20.     {  
  21.         count = array.count;  
  22.     }  
  23.       
  24.     return count;  
  25. }  
  26.   
  27. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  28. {  
  29.     // 每个row中具体视图UITableViewCell  
  30.       
  31.     // 复用标识符  
  32.     static NSString *const identifier = @"UITableViewCell";  
  33.     // 未复用标识  
  34. //    NSString *identifier = [NSString stringWithFormat:@"%@", indexPath];  
  35.       
  36.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  37.     if (cell == nil)  
  38.     {  
  39.         /* 
  40.          注意: 
  41.          实例化时样式为 UITableViewCellStyleSubtitle 时在主标题下面显示,图标在左边显示 
  42.          实例化时样式为 UITableViewCellStyleSubtitle 时在主标题下面显示,图标在左边显示 
  43.          实例化时样式为 UITableViewCellStyleValue1 时在主标题右边显示,图标在左边显示 
  44.          实例化时样式为 UITableViewCellStyleDefault 时不显示副标题,图标在左边显示 
  45.          */  
  46.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];  
  47.           
  48.           
  49.         // 设置分割线对齐(注意:与tableview的 separatorInset、layoutMargins 属性同时设置才有效)  
  50.         cell.separatorInset = UIEdgeInsetsMake(0.00.00.00.0);  
  51.         cell.layoutMargins = UIEdgeInsetsMake(0.00.00.00.0);  
  52.         // 选中时的状态  
  53.         cell.selectionStyle = UITableViewCellSelectionStyleGray;  
  54.         // 设置自定义事件图标(在右侧显示。注意:accessoryType 与 accessoryView 不能同时设置)  
  55.         UIImageView *accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellImage"]];  
  56.         accessoryView.frame = CGRectMake(0.00.020.020.0);  
  57.         cell.accessoryView = accessoryView;  
  58.         // 设置系统副件图标(在右侧显示。注意:accessoryType 与 accessoryView 不能同时设置)  
  59.         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  60.     }  
  61.       
  62.       
  63.     NSDictionary *dict = self.mainArray[indexPath.section];  
  64.     NSArray *array = [dict objectForKey:@"array"];  
  65.     NSString *text = [array objectAtIndex:indexPath.row];  
  66.   
  67.       
  68.     // 设置cell中的图标  
  69.     cell.imageView.image = [UIImage imageNamed:@"cellImage"];  
  70.     // 设置cell中的主标题  
  71.     cell.textLabel.text = text;  
  72.     // 设置cell中的副标题,在主标题下面显示  
  73.     /* 
  74.      注意: 
  75.      实例化时样式为 UITableViewCellStyleSubtitle 时在主标题下面显示,图标在左边显示 
  76.      实例化时样式为 UITableViewCellStyleSubtitle 时在主标题下面显示,图标在左边显示 
  77.      实例化时样式为 UITableViewCellStyleValue1 时在主标题右边显示,图标在左边显示 
  78.      实例化时样式为 UITableViewCellStyleDefault 时不显示副标题,图标在左边显示 
  79.     */  
  80.     cell.detailTextLabel.text = @"ios learning";  
  81.       
  82.       
  83.     return cell;  
  84. }  
  85.   
  86.   
  87. //- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  88. //{  
  89. //    // 每个section中的页眉视图的标题  
  90. //    // 设置后不能再设置 - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  91. //      
  92. //    NSString *title = [NSString stringWithFormat:@"header section %@", @(section)];  
  93. //    return title;  
  94. //}  
  95. //  
  96. //- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section  
  97. //{  
  98. //    // 每个section中的页脚视图的标题  
  99. //    // 设置后不能再设置 - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section  
  100. //      
  101. //    NSString *title = [NSString stringWithFormat:@"footer section %@", @(section)];  
  102. //    return title;  
  103. //}  

[objc] view plain copy
  1. // UITableViewDelegate  
  2.   
  3. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  4. {  
  5.     // 每个section中每个row的视图UITableViewCell的高度。默认是44.0  
  6.       
  7.     return 44.0;  
  8. }  
  9.   
  10. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  11. {  
  12.     // 每个section中页眉的高度  
  13.       
  14.     return 50.0;  
  15. }  
  16.   
  17. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  
  18. {  
  19.     // 每个section中页脚的高度  
  20.       
  21.     return 20.0;  
  22. }  
  23.   
  24. - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  25. {  
  26.     // 设置每个section的页眉视图  
  27.     // 设置后不能再设置 - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  28.       
  29.       
  30.     NSDictionary *dict = self.mainArray[section];  
  31.     NSString *title = [dict objectForKey:@"title"];  
  32.       
  33.       
  34.     // 未复用  
  35. //    UIButton *view = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 50.0)];  
  36. //    view.backgroundColor = [UIColor brownColor];  
  37. //    [view setTitle:title forState:UIControlStateNormal];  
  38. //    [view setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];  
  39. //      
  40. //    view.tag = 1000 + section;  
  41. //    [view addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];  
  42. //      
  43. //    return view;  
  44.       
  45.     // 复用  
  46.     static NSString *const identifier = @"headerview";  
  47.       
  48.     UITableViewHeaderFooterView *headerview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];  
  49.     if (headerview == nil)  
  50.     {  
  51.         headerview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identifier];  
  52.           
  53.         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10.010.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 30.0)];  
  54.         button.backgroundColor = [UIColor brownColor];  
  55.         [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];  
  56.         button.tag = 2000;  
  57.           
  58. //        button.tag = 1000 + section;  
  59.         [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];  
  60.           
  61.         [headerview.contentView addSubview:button];  
  62.     }  
  63.       
  64.     headerview.contentView.tag = 1000 + section;  
  65.     NSLog(@"headerview.tag = %@", @(headerview.tag));  
  66.       
  67.     UIButton *button = (UIButton *)[headerview.contentView viewWithTag:2000];  
  68.     [button setTitle:title forState:UIControlStateNormal];  
  69.       
  70.     return headerview;  
  71.       
  72. }  
  73.   
  74. - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section  
  75. {  
  76.     // 设置每个section的页脚视图  
  77.     // 设置后不能再设置 - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section  
  78.       
  79.       
  80.     NSDictionary *dict = self.mainArray[section];  
  81.     NSString *title = [dict objectForKey:@"title"];  
  82.       
  83.       
  84.     // 未复用  
  85. //    UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 20.0)];  
  86. //    view.backgroundColor = [UIColor yellowColor];  
  87. //    view.text = title;  
  88. //      
  89. //    return view;  
  90.       
  91.     // 复用  
  92.     static NSString *const identifier = @"footerview";  
  93.       
  94.     UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];  
  95.     if (footerView == nil)  
  96.     {  
  97.         footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identifier];  
  98.           
  99.         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.00.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 20.0)];  
  100.         label.backgroundColor = [UIColor greenColor];  
  101.         label.tag = 2000;  
  102.           
  103.         [footerView.contentView addSubview:label];  
  104.     }  
  105.       
  106.     UILabel *label = (UILabel *)[footerView.contentView viewWithTag:2000];  
  107.     label.text = title;  
  108.       
  109.     return footerView;  
  110. }  
  111.   
  112. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  113. {  
  114.     // UITableViewCell单元格选中点击  
  115.       
  116.     // 取消高亮选中状态  
  117.     [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  118.       
  119.     NSLog(@"你点击了 第 %@ 个section中的第 %@ 个row", @(indexPath.section + 1), @(indexPath.row + 1));  
  120. }  

[objc] view plain copy
  1. // 响应方法 用于展开,或折叠section  
  2. - (void)buttonClick:(UIButton *)button  
  3. {  
  4. //    NSInteger section = button.tag - 1000;  
  5.     UIView *superview = button.superview;  
  6.     NSInteger tagSuperview = superview.tag;  
  7.     NSInteger section = tagSuperview - 1000;  
  8.       
  9.     NSNumber *openNumber = self.boolArray[section];  
  10.     if (openNumber.intValue == 1)  
  11.     {  
  12.         [self.boolArray replaceObjectAtIndex:section withObject:@(0)];  
  13.     }  
  14.     else  
  15.     {  
  16.         [self.boolArray replaceObjectAtIndex:section withObject:@(1)];  
  17.     }  
  18.       
  19.     // 重新刷新数据  
  20.     // 方法1 刷新全总数据  
  21. //    [self.tableview reloadData];  
  22.     // 方法2 只刷新改变的secton  
  23.     NSIndexSet *reloadIndexSet = [NSIndexSet indexSetWithIndex:section];  
  24.     [self.tableview reloadSections:reloadIndexSet withRowAnimation:UITableViewRowAnimationNone];  
  25. }  





0 0
原创粉丝点击