zoukankan      html  css  js  c++  java
  • UITableView详解

      ios开发中,UITableView是非常非常重要的UI控件,熟练了解它的各个常用属性方法很有必要。 

      常用方法: 

     #pragma mark - DataSource
     #pragma mark 每组多少行
     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     
     #pragma mark 每组显示的内容,当出现在屏幕上得时候才加载此方法
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      
     #pragma mark 有多少组
     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     
     #pragma mark 组的头部的文字
     - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
     
     #pragma mark 组的尾部文字
     - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
     
     #pragma mark 右边索引文字
     - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    
     #pragma mark 编辑行(添加 删除)
     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    
     #pragma mark 移动行
     - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    
     #pragma mark - Delegate
     #pragma mark 每行的高度
     - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    
     #pragma mark 组的头部高度
     - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    
     #pragma mark 组的尾部高度
     - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    
     #pragma mark 自定义头部View
     - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    
     #pragma mark 自定义尾部View
     - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    
     #pragma mark 选中行
     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    
    #pragma mark 编辑模式默认是删除 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    #pragma mark - 刷新数据
    #pragma mark 刷新全局数据
    [self.tableView reloadData];

    #pragma mark 刷新删除数据
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:<#UITableViewRowAnimation#>]

    #pragma mark 刷新插入数据
    [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:<#UITableViewRowAnimation#>]

    #pragma mark 设置行的高度
    [self.tableView setRowHeight:]

    #pragma mark 设置tableView顶部的View, 常用来做顶部图片轮播
    [self.tableView setTableHeaderView:]

    #pragma mark 设置tableView底部的View, 常用来做底部上拉加载更多
    [self.tableView setTableFooterView]

      下面用一个小例子说明:

      1 @interface WYSViewController ()
      2 
      3 @property (nonatomic,strong) NSMutableArray *dataList;
      4 
      5 @end
      6 
      7 @implementation WYSViewController
      8 
      9 - (void)viewDidLoad
     10 {
     11     [super viewDidLoad];
     12     
     13     
     14     // 开启编辑模式,开启后不能选中行
     15     // self.tableView.editing = YES;
     16     
     17     // tableHeaderView定义的View, 常用来做顶部轮播图片
     18     [self tableHeaderViewData];
     19     
     20     // tableFooterView定义的View, 常用来做底部上拉加载更多
     21     [self tableFooterViewData];
     22 }
     23 
     24 #pragma mark - tableHeaderView定义的View
     25 - (void)tableHeaderViewData
     26 {
     27     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
     28     [view setBackgroundColor:[UIColor redColor]];
     29     
     30     UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 250, 30)];
     31     [lab setText:@"tableHeaderView定义的View"];
     32     
     33     [view addSubview:lab];
     34     self.tableView.tableHeaderView = view;
     35 }
     36 
     37 #pragma mark - tableFooterView定义的View
     38 - (void)tableFooterViewData
     39 {
     40     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
     41     [view setBackgroundColor:[UIColor yellowColor]];
     42     
     43     UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 250, 30)];
     44     [lab setText:@"tableFooterView定义的View"];
     45     
     46     [view addSubview:lab];
     47     self.tableView.tableFooterView = view;
     48 }
     49 
     50 #pragma mark - 加载数据
     51 - (NSMutableArray *)dataList
     52 {
     53     if (_dataList == nil){
     54         
     55 //        NSArray *arrayM = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
     56 //        _dataList = [NSMutableArray arrayWithArray:arrayM];
     57         
     58         _dataList = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
     59     }
     60     
     61     
     62     return _dataList;
     63 }
     64 
     65 #pragma mark - DataSource
     66 #pragma mark 每组多少行
     67 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     68 {
     69     
     70     NSLog(@"每组多少行");
     71     
     72     return [self.dataList count];
     73 
     74 }
     75 
     76 #pragma mark 每组显示的内容,当出现在屏幕上得时候才加载此方法
     77 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     78 {
     79     
     80     NSLog(@"每行显示的内容");
     81     
     82     static NSString *ID = @"cellID";
     83     
     84     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
     85     
     86     if (cell == nil){
     87         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
     88     }
     89     
     90     
     91     [cell.textLabel setText:self.dataList[indexPath.row]];
     92     
     93     return cell;
     94     
     95     
     96 }
     97 
     98 #pragma mark 有多少组
     99 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    100 {
    101     
    102     NSLog(@"有多少组");
    103     
    104     return 1;
    105     
    106     
    107 }
    108 
    109 #pragma mark 组的头部的文字
    110 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    111 {
    112     NSLog(@"我是组头部文字");
    113     
    114     return @"我是组的头部文字";
    115     
    116 }
    117 
    118 #pragma mark 组的尾部文字
    119 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    120 {
    121 
    122     NSLog(@"我是组尾部文字");
    123     
    124     return @"我是组的尾部文字";
    125 
    126 }
    127 
    128 
    129 #pragma mark 右边索引文字
    130 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    131 {
    132     return @[@"1",@"2",@"3",@"4",@"5"];
    133 }
    134 
    135 
    136 #pragma mark 编辑行(添加 删除)
    137 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    138 {
    139     
    140     NSLog(@"编辑模式");
    141     
    142     if (editingStyle == UITableViewCellEditingStyleDelete){
    143         
    144         [self.dataList removeObjectAtIndex:indexPath.row];
    145         
    146         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    147         
    148         // 全局刷新
    149         //[tableView reloadData];
    150         
    151     }else{
    152         
    153         [self.dataList insertObject:@"good" atIndex:indexPath.row + 1];
    154         
    155         NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
    156         
    157         [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];
    158     }
    159 }
    160 
    161 
    162 #pragma mark 移动行
    163 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    164 {
    165     
    166     NSLog(@"移动行");
    167     
    168     id source = self.dataList[sourceIndexPath.row];
    169     [self.dataList removeObjectAtIndex:sourceIndexPath.row];
    170     
    171     [self.dataList insertObject:source atIndex:destinationIndexPath.row];
    172 }
    173 
    174 #pragma mark - Delegate
    175 #pragma mark 每行的高度
    176 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    177 {
    178     NSLog(@"每行的高度");
    179     
    180     return 60;
    181 }
    182 
    183 #pragma mark 组的头部高度
    184 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    185 {
    186     NSLog(@"组的头部高度");
    187     
    188     return 50;
    189 }
    190 
    191 #pragma mark 组的尾部高度
    192 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    193 {
    194     NSLog(@"组的尾部高度");
    195     
    196     return 50;
    197 }
    198 
    199 #pragma mark 自定义头部View
    200 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    201 {
    202     NSLog(@"自定义头部View");
    203     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    204     [view setBackgroundColor:[UIColor greenColor]];
    205     
    206     UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 250, 30)];
    207     [lab setText:@"ViewForHeader定义的View"];
    208     
    209     [view addSubview:lab];
    210     return view;
    211 }
    212 
    213 #pragma mark 自定义尾部View
    214 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    215 {
    216     NSLog(@"自定义尾部view");
    217     
    218     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    219     [view setBackgroundColor:[UIColor orangeColor]];
    220     UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 250, 30)];
    221     [lab setText:@"ViewForFooter定义的View"];
    222     
    223     [view addSubview:lab];
    224     return view;
    225 }
    226 
    227 #pragma mark 选中行
    228 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    229 {
    230     NSLog(@"选中行");
    231     
    232     // 取消行的选定
    233     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    234 }
    235 
    236 #pragma mark 编辑模式默认是删除
    237 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    238 {
    239     return indexPath.row % 2 ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
    240 }
    241 
    242 
    243 @end

    打印结果:

    2015-03-24 13:04:15.244 tableView01[3197:c07] 有多少组
    2015-03-24 13:04:15.249 tableView01[3197:c07] 组的头部高度
    2015-03-24 13:04:15.251 tableView01[3197:c07] 组的头部高度
    2015-03-24 13:04:15.256 tableView01[3197:c07] 组的尾部高度
    2015-03-24 13:04:15.258 tableView01[3197:c07] 组的尾部高度
    2015-03-24 13:04:15.262 tableView01[3197:c07] 每组多少行
    2015-03-24 13:04:15.264 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.267 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.271 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.273 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.276 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.282 tableView01[3197:c07] 有多少组
    2015-03-24 13:04:15.284 tableView01[3197:c07] 组的头部高度
    2015-03-24 13:04:15.286 tableView01[3197:c07] 组的头部高度
    2015-03-24 13:04:15.290 tableView01[3197:c07] 组的尾部高度
    2015-03-24 13:04:15.292 tableView01[3197:c07] 组的尾部高度
    2015-03-24 13:04:15.294 tableView01[3197:c07] 每组多少行
    2015-03-24 13:04:15.297 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.300 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.308 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.309 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.310 tableView01[3197:c07] 每行的高度
    2015-03-24 13:04:15.313 tableView01[3197:c07] 每行显示的内容
    2015-03-24 13:04:15.316 tableView01[3197:c07] 每行显示的内容
    2015-03-24 13:04:15.318 tableView01[3197:c07] 每行显示的内容
    2015-03-24 13:04:15.326 tableView01[3197:c07] 每行显示的内容
    2015-03-24 13:04:15.328 tableView01[3197:c07] 每行显示的内容
    2015-03-24 13:04:15.329 tableView01[3197:c07] 自定义头部View
    2015-03-24 13:04:15.332 tableView01[3197:c07] 自定义尾部view

     说明,这些方法的调用顺序为:

    #pragma mark 有多少组
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    #pragma mark 组的头部高度
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    #pragma mark 组的尾部高度
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    #pragma mark 每组多少行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    #pragma mark 每行的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    #pragma mark 每组显示的内容,当出现在屏幕上得时候才加载此方法
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    最终的显示结果:

    欢迎加QQ群交流: iOS: 279096195 React Native: 482205185
  • 相关阅读:
    vmware中3中网络模式的区别
    常用 Git 命令清单 转
    inotify监控目录变化重启服务器tornado项目
    sed处理url编码解码=== web日志的url处理
    LVM 'Can’t open /dev/sdb1 exclusively. Mounted filesystem?' Problem
    Vimium使用快捷键总结
    sed 引入shell变量
    shell 除法 小数点
    selinux 导致无法启动httpd
    linux查看ssh用户登录日志与操作日志
  • 原文地址:https://www.cnblogs.com/GeekStar/p/4362484.html
Copyright © 2011-2022 走看看