因此,似乎在cellForRowAtIndexPath
之前调用了heightForRowAtIndexPath
。我目前正在使用cellForRowAtIndexPath
来计算每个单元格的高度(它们是可变高度的,使用的是UILabel
,其中包含不同数量的文本。
我很困惑,如果设置高度的方法在计算它的方法之前被调用,我怎么才能正确地设置单元格的高度。
我已经创建了一个类别的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;
}
发布于 2013-02-19 19:27:57
您需要在heightForRowAtIndexPath
中计算单元格的高度,类似于
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *theText=[[_loadedNames objectAtIndex: indexPath.row] name];
CGSize labelSize = [theText sizeWithFont:[UIFont fontWithName: @"FontA" size: 15.0f] constrainedToSize:kLabelFrameMaxSize];
return kHeightWithoutLabel+labelSize.height;
}
您可以通过设置kLabelFrameMaxSize
来设置标注大小的一些约束
#define kLabelFrameMaxSize CGSizeMake(265.0, 200.0)
然后,通过将固定高度与可变标签高度相加来返回高度。
此外,为了保持一致,您应该使用相同的方法在cellForRowAtIndexPath
中设置框架,而不是使用sizeToFitMultipleLines
- (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;
CGSize labelSize = [_customCell.myLabel.text sizeWithFont:_customCell.myLabel.font constrainedToSize:kLabelFrameMaxSize];
_customCell.myLabel.frame = CGRectMake(5, 5, labelSize.width, labelSize.height);
return _customCell;
}
发布于 2013-02-19 23:30:46
你应该用heightForRowAtIndexPath
方法计算单元格的高度。它应该不使用任何UILabel,就像JP Hribovsek的答案中的heightForRowAtIndexPath
实现一样。
然后,在cellForRowAtIndexPath
中,不要设置任何子视图的框架,也不要调用标签的sizeToFitMultipleLines
方法。相反,应实现FQCustomCell
的layoutSubviews
方法,以便为单元格的任何可能边界正确设置子视图框。在此方法中,您不应该关心文本或字体大小,只需确保标签的边距正确即可。如果在heightForRowAtIndexPath
方法中正确计算了单元格的高度,那么标签的大小将恰到好处。
发布于 2013-02-19 19:32:13
对于每个单元格,首先调用heightforRowAtIndexPath,然后调用CellForRowAtIndexPath。因此,您可以先计算高度,然后在CellForRowAtIndexPath中填充每个单元格的数据。
https://stackoverflow.com/questions/14956189
复制相似问题