充分利用了Masonry 三方库
https://github.com/xiaoniu-xie/AutoCellHeight.git
1.效果图
2.创建tableView 设置行高
table.estimatedRowHeight = 50;
3.tableViewCell的行高设置三行代码
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
4.自定义一个tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[TableViewCell reuseIdentifier]];
if (!cell) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[TableViewCell reuseIdentifier]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.content.text = newContent;
return cell;
}
5.自定义的tableViewCell中mas_equalTo就是对应的需要自适应的地方
self.content = [[UILabel alloc]init];
self.content.numberOfLines = 0;
self.content.backgroundColor = ZXNColor(70, 122, 142, 1);
self.content.textColor = ZXNColor(240, 195, 79, 1);
[self addSubview:self.content];
[self.content mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(leftImageView.mas_right).offset(10);
make.right.mas_equalTo(rightImageView.mas_left).offset(-10);
make.top.equalTo(@10);
make.bottom.equalTo(@-10);
}];