SB Cell

使用AutoLayOut根据文本显示不等高的cell

2015-12-29  本文已影响92人  iOSWoden

这篇文章主要用于兼容iOS7系统,在iOS8以上直接一句代码系统就可以帮你算出cell的高度,有兴趣的朋友可以自己搜下。以下列出这个例子的代码流程

self.aLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width-16;
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_prototyCell == nil) {
        
        _prototyCell = [tableView dequeueReusableCellWithIdentifier:@"Custom"];
    }
    
    DataEntity *dataEntity = self.dataSource[indexPath.row];
    
    if (dataEntity.cellH<=0) {
        
        _prototyCell.aLabel.text = dataEntity.title;
        dataEntity.cellH = [_prototyCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+0.5;
    }
    return dataEntity.cellH;

}

写这篇文章是为了记录自己学习这个的过程,学习这个主要是在这里看到lishichao706的专栏,这个作者写得很详细,有兴趣可以去看看

上一篇下一篇

猜你喜欢

热点阅读