Iphone 获取tableView:HeightForRowatineXpath:在tableView:CellForRowatineXpath:之后发生?

Iphone 获取tableView:HeightForRowatineXpath:在tableView:CellForRowatineXpath:之后发生?,iphone,cocoa-touch,uitableview,Iphone,Cocoa Touch,Uitableview,我有一些UITableViewCell,它们需要根据内部字符串的长度更改高度。我正在计算tableView:cellforrowatinexpath:中所需的高度,然后将其存储在变量(self.specialRowHeight)中。然后我有: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == SP

我有一些UITableViewCell,它们需要根据内部字符串的长度更改高度。我正在计算
tableView:cellforrowatinexpath:
中所需的高度,然后将其存储在变量(
self.specialRowHeight
)中。然后我有:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == SPECIAL_SECTION) {
        return self.specialRowHeight;
    }
    else {
        return 44;
    }
}
除了在
tableView:cellforrowatinexpath:
位之前调用它,所以它总是零

有没有办法解决这个问题,或者有没有其他办法


谢谢

在这里回答我自己的问题:


最初,我非常确定高度计算与
tableView:cellforrowatinexpath:
方法绑定,不能移动到其他地方。不过,通过一系列的重组,我能够将这些东西从那里转移到
tableView:heightforrowatinexpath:
,这解决了所有问题

对于其他试图让自动高度调整单元格正常工作的人,以下代码可能会有所帮助:

// Inside tableView:cellForRowAtIndexPath:
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = self.numberOfTextRows;
// numberOfTextRows is an integer, declared in the class

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGSize theSize = [theObject.theStringToDisplay sizeWithFont:[UIFont systemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
    // This gets the size of the rectangle needed to draw a multi-line string
    self.numberOfTextRows = round(theSize.height / 18);
    // 18 is the size of the font used in the text label
    // This will give us the number of lines in the multi-line string

    if ((indexPath.section == FIXED_HEIGHT_SECTION) || (self.numberOfTextRows < 2)) {
        return 44;
        // 44 is the default row height; use it for empty or one-line cells (or in other table sections)
    } else {
        return theSize.height + 16;
        // 16 seems to provide a decent space above/below; tweak to taste
    }
}
//表视图内部:cellForRowAtIndexPath:
cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
cell.textlab.numberOfLines=self.numberOfTextRows;
//numberOfTextRows是一个整数,在类中声明
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
CGSize theSize=[theObject.theStringToDisplay sizeWithFont:[UIFont systemFontOfSize:18.0f]约束的大小:CGSizeMake(265.0f,9999.0f)lineBreakMode:UILineBreakModeWordWrap];
//获取绘制多行字符串所需的矩形大小
self.numberOfTextRows=圆形(size.height/18);
//18是文本标签中使用的字体大小
//这将给出多行字符串中的行数
if((indexPath.section==固定高度部分)| |(self.numberOfTextRows<2)){
返回44;
//44是默认行高;将其用于空单元格或单行单元格(或其他表格部分)
}否则{
返回size.height+16;
//16似乎在上面/下面提供了一个合适的空间;根据口味调整
}
}

如果你能想出一个更准确的方法来计算合适的单元格高度,我洗耳恭听。:)

您只需将self.numberOfLines设置为0一次,其工作原理相同。(如果numberOfLines设置为0,则标签使用所需的行数。)

我的解决方案是在
tableView:HeightForRowatineXpath:
的实现中调用
tableView:CellForRowatineXpath:
,如下所示

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

   [self tableView:aTableView cellForRowAtIndexPath:indexPath];
   return headerHeight;
}

我不太清楚为什么要将矩形高度除以18,然后再乘以它,但除此之外,我的方法基本相同。第一个除法是找出需要多少行,这样我就可以正确设置
cell.textlab.numberOfLines
;如果我不设置它,它会全部打印在一行上。但是是的,我想我可以在最后一位使用
size.height
,而不是
self.numberOfTextRows
。为了清晰和简化,编辑了答案。现在工作得很好。只有当单元格的唯一内容是文本标签时,这才能正常工作。sizeWithFont已被弃用。嗯。。。numberOfTextRows是一个自定义的类属性,所以除了我告诉它的以外,它不会做任何事情。您是在考虑cell.textlab.numberOfLines吗?无论如何,可以将
cell.textlab.numberOfLines
设置为
0
,但找出将使用的实际数字(例如,用于计算适当的行高)仍然很有用。我还发现生成的代码更加清晰,因为“0”在语义上一眼就看不出有任何帮助。
tableView:heightForRowAtIndexPath:
是为tableView中的每一行(!)调用的。因此,不应使用昂贵的调用,如
tableView:cellForRowAtIndexPath:
。不确定为什么会这样做,因为此方法允许您根据单元格的实际高度进行测试,即使标签不是唯一的内容。然而,这是非常昂贵的。