美文网首页
ios8之后tableview的变化

ios8之后tableview的变化

作者: 冷武橘 | 来源:发表于2021-02-26 14:39 被阅读0次

一、iOS8:Self Sizing Cells

iOS8之后苹果推出的一个新特性Self Sizing Cells,意思就是让cell自己计算自己的高度,当我们在cell里面添加完所需控件,并约束好位置之后,我们只需要设置
tableView.estimatedRowHeight = 44.0f;//推测高度,必须有,可以随便写多少
tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默认就是这个值,可以省略
这两句代码之后,即可放心的往cell的控件里面加上内容,cell会根据内部所有控件的高度动态的计算自己的高度从而显示出来。
自适应高度的关键:

  • 1、设置合适的estimatedRowHeight
  • 2、设置.rowHeight 为UITableViewAutomaticDimension
  • 3、使cell的上、下边缘黏着子控件(当然子控件必须是自动布局)
    通过重写cell:- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority可以验证cell最终是通过调用这个方法来获取cell的高度的。但是系统并没有缓存cell高度,我们可以这样
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   Model * model = _dataArray[indexPath.section];
    return model.cell_height?:UITableViewAutomaticDimension;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
   Model *model = self.dataArr[indexPath.row];
    //高度缓存
    if (model.cell_height == 0) {
        CGFloat height = cell.height;
        model.cell_height = height;
    }
}

二、IOS-tableview滑动删除

详细见:https://www.jianshu.com/p/00f7d061a807

三、 IOS11之后的适配

2.1、默认开启了 Self-Sizing

IOS11以后tableview默认开启了 Self-Sizing,导致tableview
的 estimatedRowHeight 、 estimatedSectionHeaderHeight 、 estimatedSectionFooterHeight 的高度估算属性由默认的0变成 UITableViewAutomaticDimension;如果需要禁用Self-Sizing,你需要设置estimatedRowHeight、 estimatedSectionHeaderHeight 、 estimatedSectionFooterHeight 为0;

2.2、分组头部和尾部方法

如果不实现 -tableView: viewForHeaderInSection: 和 tableView: viewForFooterInSection: 方法,则 -tableView: heightForHeaderInSection: 和 - tableView: heightForFooterInSection: 不会被调用

2.3、 safe area

1769276-c15df100f81579ca.jpg

自定义的 header 上面有一个 lable,自定义的 cell 上面也有一个 label。将屏幕横屏之后会发现,cell 以及 header 的布局均自动留出了 safe area 以外的距离。cell 还是那么大,只是 cell 的 contnt view 留出了相应的距离。这其实是 UITableView 中新引入的属性管理的:

@property (nonatomic) BOOL insetsContentViewsToSafeArea API_AVAILABLE(ios(11.0), tvos(11.0)); // default value is YES

也就是说:在 iOS 11 下, 并不需要改变 header/footer/cell 的布局, 系统会自动区适配 safe area.当然如果你并不期望这样的效果,你需要设置

tableView.insetsContentViewsToSafeArea = NO

2.4、performBatchUpdates:completion:

- (void)beginUpdates; - (void)endUpdates;被废弃,用performBatchUpdates:completion:代替

   [self.mMutableArray addObject:@0];
    [self.mMutableArray removeLastObject];
    [self.tableview performBatchUpdates:^{
        [self.tableview insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.mMutableArray.count-1 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
       [self.tableview deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.mMutableArray.count-1 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
    } completion:nil];

四、ios13:UITableViewDiffableDataSource

在 iOS 13 中 Apple 为 UITableView 和 UICollectionView 引入了 DiffableDataSource,让开发者可以更简单高效的实现 UITableView、UICollectionView 的局部数据刷新。新的刷新的方法为 apply,通过使用 apply 方法无需计算变更的 indexPaths,也无需调用 reload,即可安全地在主线程或后台线程更新 UI, 仅需简单的将需要变更后的数据通过 NSDiffableDataSourceSnapshot 计算出来。
详细见:https://www.jianshu.com/p/64b6b9407624

相关文章

网友评论

      本文标题:ios8之后tableview的变化

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