美文网首页
UITableView 详解

UITableView 详解

作者: 文子飞_ | 来源:发表于2021-02-03 18:24 被阅读0次

    UITableView 作为 iOS 中最常见的控件 ,在APP中出现频率颇高,作为第一篇文章,就索性将其整理一下


    目录

    1. UITableView的基本概念
    2. UITableView的最基本属性
    3. UITableViewCell的基本属性
    !!. UITableView要分组,创建时一定要指定Grouped样式。initWithFrame:style:UITableViewStyleGrouped。否则reloadData出现界面数据显示缺少或其他异常

    UITableView 的基本概念

    • UITableView 是用于展示列表信息的非常方便与常用的工具。
    • UITableView 是 UIScrollView 的子类 ,因此具有能够滑动的特性,能够上下滑动,同时,在cell是可编辑的情况下,能向左滑动(后面cell中会有讲解)。

    UITableView 的基本属性

    • UITableView分为两种样式

      • UITableViewStylePlain

        image
    • UITableViewStyleGrouped

      image
    • 使用时,需准守的协议
      <UITableViewDelegate,UITableViewDelegate>

      • 按照协议去写返回多少个分区,每个分区返回多少行,每行显示什么,每行多高,每个分区分区头多高,分区尾多高之类的
    • 关于 表头 表尾 分区头 分区尾

    需要注意的是 分区头和分区尾是会悬停的

    • 表头 tableHeaderView 在tableView最上面,和tableView一起滚动
    • 表尾 tableFooterView 在tableView最下面,一般用来去掉多余的cell的分割线
    tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    
    • 分区头 分区头一旦到达tableView顶部就会悬停在最上方,直到该分区全部消失

    • 分区尾 分区尾一旦出现在tableView底部就会悬停在最下方,直到该分区全部显示

      image
    • 关于tableViewCell的重用

    需要注意的是 tableView 必须使用重用机制 , 当cell滑出屏幕的时候 cell会被系统回收,在下面cell将要显示时会调用之前回收的cell,这样就不必每次都创建销毁,节省内存同时节省时间,如果不使用重用机制,每次都创建新的cell 是非常消耗内存和性能的,因此,使用重用是非常非常重要的

       UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
       if (cell == nil) {
           cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
       }
    
    

    UITableViewCell的基本属性

    • cell 样式

      • UITableViewCellStyleDefault 默认样式 没有副标题

        image
      • UITableViewCellStyleValue1

        image
      • UITableViewCellStyleValue2

        image
      • UITableViewCellStyleSubtitle

        image
    • cell分割线

      • 位置 颜色
    // 设置端距,这里表示separator离左边和右边均80像素
    tableView.separatorInset = UIEdgeInsetsMake(0,80, 0, 80); 
    tableView.separatorStyle =
    UITableViewCellSeparatorStyleSingleLine;
    tableView.separatorColor = [UIColor blueColor];
    
    
    image
    • cell选中样式
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    /*
    我一般会取消选中 也就是使用UITableViewCellSelectionStyleNone
    总是感觉有那个选中状态很丑
    这里就不一一截图列举选中样式了
    UITableViewCellSelectionStyleNone
    UITableViewCellSelectionStyleBlue
    UITableViewCellSelectionStyleGray
    UITableViewCellSelectionStyleDefault
    */
    
    
    • cell右边指示样式
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    /*
    UITableViewCellAccessoryNone 没有
    UITableViewCellAccessoryDisclosureIndicator 箭头
    UITableViewCellAccessoryDetailDisclosureButton 感叹号和箭头
    UITableViewCellAccessoryCheckmark 对勾
    UITableViewCellAccessoryDetailButton 感叹号
    */
    
    
    • cell左侧滑动编辑

    一定要记得删除数据源中的数据,否则指示表面上删除了这一行,下次读取的时候,这行数据又会出现

    
    //左滑之后会出现的字
    
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
        {
        return @"删除";
        }
    
    //点击了删除 会调用这个方法
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
        {
        // 从数据源中删除
        [_data removeObjectAtIndex:indexPath.row];
        // 从列表中删除
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
        }
    
    //退出了编辑模式会走这个方法
    
    - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    
    }
    
    
    左滑删除
    • 自定义cell
      在cell重用那里使用自定义的cell来创建就行了,记得使用重用。
      自定义的cell高度可能不是默认的44,因此,要记得修改cell的高度
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
        return 44;
        }
    
    
    • 隐藏分区头高度,分区尾高度:CGFLOAT_MIN
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == 0) {
            return 55;
        }
        return CGFLOAT_MIN;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        if (section == 0) {
            return 45;
        }
        return CGFLOAT_MIN;
    }
    
    • iOS11 UITableView刷新后 抖动(乱跳)
    • iOS 11系统下,TableView 调用reloadData方法,会导致整个界面跳动、闪屏现象,拖拉到某个位置,在根据接口返回数据加载刷新tableView,效果更惨。。iOS 11之前系统不会出现闪屏跳动现象
    // 先设置这三个属性
    if (@available(iOS 11.0, *)) { 
       _tableView.estimatedRowHeight = 0;
      _tableView.estimatedSectionHeaderHeight = 0;
       _tableView.estimatedSectionFooterHeight = 0;
    }
    // 刷新代码放在performWithoutAnimation里
    [UIView performWithoutAnimation:^{
        [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
    }];
    

    本文没有写到使用 xib 的情况 ,因为个人不太喜欢使用xib。虽然用xib会快一些,但是在后期维护方面并不是十分方便,我也建议大家多使用全代码的形式来写项目。
    本文中还有很多没有提及的东西,有一些是忘记了的,如果有疑问直接留言,我会及时添加和修改,谢谢

    
    作者:小_黑_屋
    链接:https://www.jianshu.com/p/aa9721e4484d
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    

    相关文章

      网友评论

          本文标题:UITableView 详解

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