所以,heightForRowAtIndexPath
似乎在cellForRowAtIndexPath
之前被调用。我目前使用cellForRowAtIndexPath
来计算每个单元格的高度(它们是可变高度,使用UILabel
以及不同数量的文本)。在cellForRowAtIndexPath中调用heightForRowAtIndexPath?
我很困惑,至于如何正确设置单元格的高度如果套高度的方法之前调用该方法计算它。
我创建的NSString
类别,另一个UILabel
,这共同努力使UILabel
大小合适。这个问题是包含这些标签的UITableViewCell
实例没有被调整大小,所以标签挂在每个标签的末尾,重叠在下面的标签上。
我知道我需要做的是使单元格的高度与标签的高度相等,再加上一些额外的空白空间,但我很疑惑我如何在标签高度设置之前设置高度已计算。我可以在不使用heightForRowAtIndexPath
的情况下设置某个人的身高吗?如果不是,我可以计算其他地方的细胞高度吗?我目前使用indexPath.row
来实现这一点,所以事先做好会很棘手。下面的代码我的cellForRowAtIndexPath:
的cellForRowAtIndexPath
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
if (_customCell == nil) {
_customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
}
_customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
_customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];
UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
backView.backgroundColor = [UIColor clearColor];
_customCell.backgroundView = backView;
_customCell.myLabel.frame = CGRectMake(5, 5, 265, 65);
[_customCell.myLabel sizeToFitMultipleLines];
return _customCell;
}
从iOS 8开始,您还可以使用自动计算的动态行高。 HTTP://计算器。com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Suragch 2016-04-12 06:21:00