Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone sizeWithFont方法的神秘问题_Iphone_Ios_Xcode_Uitableview_Nsstring - Fatal编程技术网

Iphone sizeWithFont方法的神秘问题

Iphone sizeWithFont方法的神秘问题,iphone,ios,xcode,uitableview,nsstring,Iphone,Ios,Xcode,Uitableview,Nsstring,伙计们。 我有不同单元格的UITableView,还有计算高度的代码。在一个项目中,它工作正常,但在第二个项目中,它返回的高度等于0。 这可能是什么原因造成的? 我的代码: 在调用cellforrowatinexpath:delegate方法之前,调用heightforrowatinexpath:delegate方法。在代码中,您是根据cell.textlab.text计算单元格高度,但单元格中的文本尚未设置 您需要从表格单元格以外的其他位置获取此方法中要使用的文本(当您在CellForRowa

伙计们。 我有不同单元格的UITableView,还有计算高度的代码。在一个项目中,它工作正常,但在第二个项目中,它返回的高度等于0。 这可能是什么原因造成的? 我的代码:


在调用
cellforrowatinexpath:
delegate方法之前,调用
heightforrowatinexpath:
delegate方法。在代码中,您是根据cell.textlab.text计算单元格高度,但单元格中的文本尚未设置


您需要从表格单元格以外的其他位置获取此方法中要使用的文本(当您在CellForRowatineXpath中设置textlabel.text值时,可能可以从任何获取文本的位置获取该文本).

在调用
cellforrowatinexpath:
委托方法之前调用
heightforrowatinexpath:
委托方法。在代码中,您是根据cell.textlab.text计算单元格高度,但单元格中的文本尚未设置


您需要从表格单元格以外的其他位置获取此方法中要使用的文本(当您在CellForRowatineXpath中设置textlabel.text值时,可能可以从任何获取文本的位置获取该文本)。

+1表示正确答案-首先调用HeightForRowatineXpath,然后调用CellForRowatineXpath。为什么OP以前能看到这种效果对我来说仍然是一个谜。谢谢你,我想,这个问题更难回答。+1为正确答案-首先调用HeightForRowatineXpath,然后调用CellForRowatineXpath。为什么OP以前看到过这种效果对我来说仍然是个谜。谢谢你,我想,这个问题更难解决。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGFloat cellWidth = 320.0f;
    CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cellWidth, CGFLOAT_MAX) lineBreakMode:cell.textLabel.lineBreakMode];
    CGFloat height = size.height;
    NSLog(@"Height: %f", height);
    return height;
}