iOS UI相关之UITableView的高度计算02-下

OK, 在了解了如何计算tableView的高度之后,是不是应该了解一下更加简单的办法计算高度以及高度计算方面的优化工作呢

通过xib约束进行cell便捷高度计算
  • 1,创建xib后进行正确的约束:
    正确的意思是:内部的空间必须要把view的本身高度撑起来,如下
Snip20170113_3.png
  • 2,cell内部接受到数据后只需要进行简单的赋值即可
- (void)setStatus:(IMStatus *)status{
    _status = status;
    self.iconView.image = [UIImage imageNamed:status.icon];
    self.nameLabel.text = status.name;
    self.vipView.image = [UIImage imageNamed:@"vip"];
    self.textView.text = status.text;
}
  • 3, 在控制器中也比较简单
  • 从xib中加载,并设置estimatedRowHeight,其值可以是非零的任意值
    [self.tableView registerNib:[UINib nibWithNibName:@"IMTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
//    self.tableView.rowHeight = UITableViewAutomaticDimension;//ios8以后这个是默认值可以不设置
    self.tableView.estimatedRowHeight = 5//只需要非零即可
  • cellForCell方法内部也就是接受下数据即可
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.status = _statuses[indexPath.row];
    return cell;
}
优化

优化方法是用的百度iOS孙源的框架,具体的内容我觉得他讲的非常好,直接把地址拿过来了
UITableView+FDTemplateLayoutCell
优化UITableViewCell高度计算的那些事

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(IMTableViewCell *cell) {
        // 配置 cell 的数据源,和 "cellForRow" 干的事一致,比如:
        cell.status = _statuses[indexPath.row];
    }];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容