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