我已经看到了许多有关此问题的问题和答案,但没有任何效果。我在tablview中有两个自定义单元格。我想根据内容更改单元格的高度。我现在尝试使用此代码,但是它永远无法正常工作。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell;
NSLog(@"%ld",(long)indexPath.row);
NSLog(@"%ld",(long)hospital.hospComment.count);
if (hospital.hospComment.count > (indexPath.row+1)) {
cell.personComment.text = [hospital.hospComment objectAtIndex:(indexPath.row +1)];
}
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
height += 1;
return height;
}
我怎样才能做到这一点?
您应该像这样声明单元格,
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCellMain"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableViewCellMain"];
}
声明单元格中的Accordint将为nil。第二件事,请确保您给出适当的约束
还有一个建议:您可以使用Apple的新功能UITableViewAutomaticDimension
和estimatedRowHeight
。要了解更多有关它的信息,请参阅Appcoda的这篇很棒的小教程。
希望这会有所帮助:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句