tableview cell自适应无需计算

充分利用了Masonry 三方库
https://github.com/xiaoniu-xie/AutoCellHeight.git
1.效果图

张小牛.png

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);
  }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容