cell 自適應(yīng)高度

cell自適應(yīng)高度

1.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath里直接算出cell的高度
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * 10, MAXFLOAT);
// CGFloat textH = [topic.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize].height;//方法棄用
CGFloat textH = [topic.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;

每加載一次````cell```` 就算一次,如果````cell````比較多  就會(huì)出現(xiàn)偶爾卡頓的情況

2.把計(jì)算cell 的高度寫 到模型里 這樣 cell的高度酒只算了一次
- (CGFloat)cellHeight
{
if (!_cellHeight) {
// 文字的最大尺寸
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * XMGTopicCellMargin, MAXFLOAT);
// 計(jì)算文字的高度
CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;

    // cell的高度
    _cellHeight = XMGTopicCellTextY + textH + XMGTopicCellBottomBarH + 2 * XMGTopicCellMargin;
}
return _cellHeight;

}

還有一種更簡(jiǎn)單的方法
  1. label 的約束必須要有距離底部的高度
  2. self.tabView.estimatedRowHeight = 40;//必須要有估計(jì)高度
  3. self.tabView.rowHeight = UITableViewAutomaticDimension;
這是iOS8.0 以后的方法  適用于 所有的cell 都一樣  那種
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容