iOS笔记之UITableView自动计算cell高度
方法一:使用系统自动计算cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =
[self tableView:tableView cellForRowAtIndexPath:indexPath];
return [cell.contentView systemLayoutSizeFittingSize:
UILayoutFittingCompressedSize].height;
}方法二:使用第三方库下载地址:UITableView-FDTemplateLayoutCell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellReuseId = [self getReuseIdWithIndexPath:indexPath]; //获取cell id
CGFloat height = [tableView fd_heightForCellWithIdentifier:cellReuseId cacheByIndexPath:indexPath configuration:^(id cell) {
[self configureCell:cell indexPath:indexPath]; //对cell进行赋值
}];
return height;
}tips:使用fd的时候需要注意,cell在竖直方向的约束必须要撑满举例说明:cell里面有一个label,进行约束设置.
错误示范:
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.contentView);
make.centerY.equalTo(self.contentView); //竖直方向约束是不满的,虽然label会有一个本身的高度
}正确做法:法一:
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.contentView);
make.centerY.equalTo(self.contentView);
make.top.bottom.equalTo(self.contentView); //约束撑满竖直方向
}法二:
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView);
}]; 相关推荐
heqiang0 2020-06-25
Terminator0 2020-02-19
WintonTalks 2012-07-16
莫封洛 2019-07-26
ssddgkke 2019-06-30
我的iOS王者之路 2019-06-28
Rephontil 2015-08-10
NineYao 2015-01-30
wsmrcool 2013-01-17
SoccerZZM 2012-10-15
NineYao 2012-07-16
Terminator0 2019-11-11
lijiexiaoge 2019-06-29
jscjxysx 2019-06-26
Jplane 2019-06-26
Jplane 2019-06-20