常用的第三方iOS开发自己尝试等装一下

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

2017-01-13  本文已影响53人  张不二01

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

通过xib约束进行cell便捷高度计算
Snip20170113_3.png
- (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;
}
    [self.tableView registerNib:[UINib nibWithNibName:@"IMTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
//    self.tableView.rowHeight = UITableViewAutomaticDimension;//ios8以后这个是默认值可以不设置
    self.tableView.estimatedRowHeight = 5//只需要非零即可
- (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];
    }];
}
上一篇 下一篇

猜你喜欢

热点阅读