iOS-tableViewCell根据label字体多少自适应高度


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (cell == nil)

{

cell = [[ThirdTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

cell.index = indexPath;

cell.returnIndexPath = ^(NSIndexPath *indexPath){

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

};

ThirdTableViewCellModel *model = self.tittleArr[indexPath.row];

[cell configCellWithModel:model];

return cell;

}

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

{

ThirdTableViewCellModel *model = _tittleArr[indexPath.row];

return [ThirdTableViewCell heightWithModel:model];

}


下面是在自定义cell中实现

#pragma mark - 赋值

-(void)configCellWithModel:(ThirdTableViewCellModel *)model

{

self.tittleLabel.text = model.tittle;

self.detailLabel.text = model.detail;

}

#pragma mark - 根据model的内容,返回cell的高度

+ (CGFloat)heightWithModel:(ThirdTableViewCellModel *)model

{

ThirdTableViewCell *cell = [[ThirdTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

[cell configCellWithModel:model];

[cell layoutIfNeeded];

CGRect frame =  cell.detailLabel.frame;

NSLog(@"===height=%.2f  ===y=%.2f",frame.size.height,frame.origin.y);

return frame.origin.y + frame.size.height + 15;

}

DEMO下载地址:https://github.com/xiaohuangge/TableViewCellLayoutHeight

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

推荐阅读更多精彩内容