美文网首页iOS 开发 iOS Developer
iOS-tableViewCell根据label字体多少自适应高

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

作者: Bruce_XHG | 来源:发表于2016-08-13 12:49 被阅读0次

- (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

相关文章

网友评论

    本文标题:iOS-tableViewCell根据label字体多少自适应高

    本文链接:https://www.haomeiwen.com/subject/rkiesttx.html