美文网首页
UITableView

UITableView

作者: Barry_小闪 | 来源:发表于2016-05-22 16:31 被阅读176次

    目录

    • 设置协议
    • 创建cell
    • 用nib创建cell
    • UITableViewDataSource
    • UITableViewDelegate
    • 常用属性
    • 不常用属性
    • TableView滚动时调用的方法
    • titles索引列

    UITableView 继承 UIScrollView
    //获取选中的cell
    self.myTableView.indexPathForSelectedRow.row

    设置协议

    1.创建tableView对象

     UITableViewStylePlain,  (默认)扁平风格(也是可以分组)
     UITableViewStyleGrouped  分组
    
     tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
     //2.显示在界面上
     [self.view addSubview:tableView];
    
    补充:UICollectionView底部被标签了控制器(tabBar)遮挡的解决办法
    //
     self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-44) collectionViewLayout:flowLayout];
        
     self.automaticallyAdjustsScrollViewInsets = NO;
     self.collectionV.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
    

    3.设置代理
    数据相关的代理(只有实现dataSource的协议方法,才能在tableView上去显示数据)

    //声明协议
     @interface ViewController ()<UITableViewDataSource>
      
    //设置代理
     tableView.dataSource = self;
    

    UITableViewDataSource

    必须实现的协议方法

    设置每组的个数

    返回值:设置分组中的行数(每组cell的个数)
    参数1:委托
    参数2:第几组

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        //需要每一组显示100条数据
        return 100;
    }
    
    Cell(tableView显示数据,也不是通过tableView本身去显示数据,而是通过cell来显示)

    返回值:创建好的cell
    参数1:委托
    参数2:cell的位置(和坐标没有关系,只和组和行有关)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
      !!!!!面试过程中常问的问题:
      1.去复用池中查看是否有可以复用(重复使用)的cell;如果有就返回可以复用的cell地址,没有返回nil
       参数:复用ID
     UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
        
      2.判断是否拿到可以复用cell,如果没有拿到就创建一个新的cell(最终创建cell的个数是整屏显示的cell的个数加1或者加2)
        if (cell==nil) {
        参数1:风格
        参数2:复用ID
    
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellId"];
        }
    
      3.刷新数据(刷新cell上显示的内容)
       indexPath由section(组)和row(行)组成
        cell.textLabel.text = [NSString stringWithFormat:@"第%ld组,第%ld行", indexPath.section, indexPath.row];
        
       4.返回cell
       return cell;
        
    }
    

    设置cell的选中样式

    //UITableViewCellSelectionStyleNone没有选中的效果
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

    nib定制Cell

    让TableView先注册nib

     // 注册单元格
     [_tableView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellReuseIdentifier:SubjectCellIdentifier];
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // 复用单元格
      SubjectCell * cell = [tableView dequeueReusableCellWithIdentifier:SubjectCellIdentifier forIndexPath:indexPath];
        
      // 获取数据模型
      SubjectModel * model = self.subjectArray[indexPath.row];
        
      cell.model = model;
        
      return cell;
    }
    

    设置tableView的分组数(默认是1个分组)
    参数:委托

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
        return 3;  
    }
    

    可选协议

    设置header的标题

    - ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
        switch (section) {
            case 0:
                return @"热门";
                break;
                
            case 1:
                return @"最近更新";
                break;
            
            case 2:
                return @"销量最高";
                break;
                
            default:
                break;
        }
        return @"头标题";
    }
    

    设置footer的标题

    - ( NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    
        return @"脚标题";
        
    }
    

    UITableViewDelegate

    MARK高度相关

    设置Cell行高

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
            return 50; 
    }
    
    选中相关

    选中一个cell的时候会调用这个方法

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        NSLog(@"cell被选中");
        //在这儿跳转到下一个界面
        NextViewController * next = [[NextViewController alloc] init];
        
        //传值
        next.indexpath = indexPath;
        
        
        //push到下一个界面
        [self.navigationController pushViewController:next animated:YES];
        
        //present到下一个界面
        [self presentViewController:next animated:YES completion:nil];
        
    }
    
    头视图和脚视图相关

    设置header高度

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //    return 50 * section + 10;
        return 50;
    }
    

    设置Footer高度

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
        return 50;
    }
    
    

    设置每一组的headerView

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
       //创建一个视图(设置frame无效)
       UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
       //设置背景颜色
       headerView.backgroundColor = [UIColor redColor];
            
       //创建一个label
       UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
       label.text = [NSString stringWithFormat:@"头标题:%ld", section];
       [headerView addSubview:label];
            
       return headerView;
        
    }
    

    设置每组脚视图Footer

    - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
        //创建一个视图(设置frame无效)
        UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        //设置背景颜色
        headerView.backgroundColor = [UIColor yellowColor];
        
        
        //创建一个label
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
        label.text = @"脚标题";
        [headerView addSubview:label];
        
        
        return headerView;
        
    }
    

    Cell的附件相关

    设置指定位置的cell的附件类型
    
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        UITableViewCellAccessoryNone,(默认)
        UITableViewCellAccessoryDisclosureIndicator 一个小箭头(常用)
        UITableViewCellAccessoryDetailDisclosureButton 一个详情按钮加小箭头
        UITableViewCellAccessoryCheckmark,  一个蓝色勾
        UITableViewCellAccessoryDetailButton  一个详情按钮
          
    
    添加完箭头以后,若还想在箭头前面添加文字

    http://www.th7.cn/Program/IOS/201410/289058.shtml

    
    cell.detailTextLabel.text = @"mona";
    如果你执行之后可以看到箭头前有文字出现的话,那么恭喜你,你是幸运的。
    我就没那么幸运,改了很多次之后都cell右侧都没有显示过文字出来,后来就根据这个情况,找了很久都没找到相应的解决方法。
    后来在一段demo中才知道,原来要在右侧显示出detailTextLabel的文字,还需要在cellForRowAtIndexPath:中把cell的类型设置为
    
    UITableViewCellStyleValue1,cellForRowAtIndexPath:的整段代码如下:
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"formCell"];    
     if (cell == nil) {       
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"formCell"];       
     cell.textLabel.text = @"monalisa";   
      }    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailTextLabel.text = @"mona";
    
    
    附件上的按钮被点击后调用这个方法
    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    
        NSLog(@"附件按钮被点击");
    }
    
    将要显示一个cell的时候会调用这个方法
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
        NSLog(@"将要显示cell");
    }
    

    常用属性

    1.行高(每一行的高度都是200)

     tableView.rowHeight = 200;
    

    2.分组的header和footer的高度

     tableView.sectionHeaderHeight = 100;
     tableView.sectionFooterHeight = 100;
    

    3.设置分割线的样式

     UITableViewCellSeparatorStyleNone,     (隐藏分割线)
     UITableViewCellSeparatorStyleSingleLine,
     UITableViewCellSeparatorStyleSingleLineEtched
       
     [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    

    4.手动刷新数据(自动刷新是在创建和滑动tableView的时候)---刷新会重新调用dataSource中创建cell的方法去重新创建cell

    //刷新所有的cell
    [tableView reloadData];
    
    
     //刷新指定的组
     NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:5];
     [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    
    //刷新指定的row
    [self.tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]
    
    

    5.将视图添加到Header上,只能添加一个

     UIView * view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
     
     view.backgroundColor = [UIColor yellowColor];
        [tableView setTableHeaderView:view];
    

    6.设置内容偏移

    [tableView setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)];
    

    7.获取cell在当前TabView中的indexPath

     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    

    8.点击后取消cell选中效果

    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {    
          // 取消选中状态  
          [tableView deselectRowAtIndexPath:indexPath animated:NO];
     }
    
    

    不常用属性

    1.设置分割线的边距

    [tableView setSeparatorInset:UIEdgeInsetsMake(50, 50, 50, 50)];
    

    2.设置分割线的颜色

      [tableView setSeparatorColor:[UIColor redColor]];
    

    3.获取指定的坐标点所在的位置(第多少组第几行)

     - ( NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
    

    4.获取到指定位置(第几组第几行)对应的cell

     - ( UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    5.获取当前界面上可见的所有的cell

     //@property (nonatomic, readonly) NSArray *visibleCells;
    

    6.d

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
    

    6.滚动到指定位置
    参数1: 指定的位置
    参数2: 滚动位置

    - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
    

    7.将选中的cell滚动到指定位置(顶部底部或者中间)

    - (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
    

    8.刷新指定位置的cell

    - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    

    9.选中指定的位置

    - (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
    

    TableView滚动时调用的方法

    当TableView滚动时会调用该方法

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    

    当TableView停止滚动时会调用该方法

     - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
    

    titles索引列

    //设置背景颜色
     self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    //设置字体颜色 
    self.tableView.sectionIndexColor = [UIColor blackColor];
    
    
    
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        return self.indexArray;
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        
        NSString *key = [self.indexArray objectAtIndex:section];
        return key;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {
        return index;
    }
    
    

    相关文章

      网友评论

          本文标题:UITableView

          本文链接:https://www.haomeiwen.com/subject/kqnorttx.html