iOS 非等高 Cell

2015-06-05  本文已影响234人  雪波

将文本内容设置为自动换行

    // 设置label每一行文字的最大宽度
    // 为了保证计算出来的数值 跟 真正显示出来的效果 一致
    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;

设置自定义cell高度

    [self layoutIfNeeded];  // 强制布局

    if (status.picture) {
        status.cellHeight = CGRectGetMaxY(self.pictureView.frame) + 10;
    }else {
        status.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + 10;
    }

之所以先返回一个大约高度,因为这样能避免 heightForRowAtIndexPath 方法在 cell 被创建之前就多次调用.这样既不会得不到 cell 的高度,也能一定程度优化程序性能.

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 200;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [self.statuses[indexPath.row] cellHeight];
}
上一篇 下一篇

猜你喜欢

热点阅读