10(二)cell自适应高度和缓存高度

方法一:

cell根据文字的多少自适应高度:

- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *text = [_listArray objectAtIndex: indexPath.row];

//320为文字显示的宽度,高度1000是随便写的,会自动根据文字的大小和宽度计算出高度

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14]constrainedToSize:CGSizeMake(320,1000)];

// +20是为了让每个cell之间有些间隔

return size.height+20;

}


方法二:

//设置table的预估行高

self.tableView.estimatedRowHeight = 200;

//设置行高自动计算

self.tableView.rowHeight = UITableViewAutomaticDimension;

//自动计算行高的关键:给contentView的底部设置一个向上的约束

[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.left.right.equalTo(self);

make.bottom.equalTo(separatorImage).offset(8);

}];

你可能感兴趣的:(10(二)cell自适应高度和缓存高度)