Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
如何处理具有不同单元格高度的UITableView?_Uitableview_Delegates_Height - Fatal编程技术网

如何处理具有不同单元格高度的UITableView?

如何处理具有不同单元格高度的UITableView?,uitableview,delegates,height,Uitableview,Delegates,Height,表中的每个单元格都有一个标签,最多有三行,因此单元格高度取决于标签的高度,我知道为特定单元格返回高度的委托方法(NSIndexPath),但我需要获取实际单元格以确定高度,如果我调用cellForRow:atIndexPath:in heightForCell:,它将无限循环,我猜cellForRow也叫heightForCell。那我有什么办法处理这件事吗 谢谢 您可以在heightForRow:delegate方法中为字符串使用NSStrings size,例如 CGSize maxSize

表中的每个单元格都有一个标签,最多有三行,因此单元格高度取决于标签的高度,我知道为特定单元格返回高度的委托方法(NSIndexPath),但我需要获取实际单元格以确定高度,如果我调用cellForRow:atIndexPath:in heightForCell:,它将无限循环,我猜cellForRow也叫heightForCell。那我有什么办法处理这件事吗


谢谢

您可以在heightForRow:delegate方法中为字符串使用NSStrings size,例如

CGSize maxSize = CGSizeMake(300, 800); //max x width and y height
NSString *cellTitle = @"Lorem ipsum";
UIFont *stringFont = [UIFont systemFontOfSize:14]; // use the same font your using on the cell
CGSize cellStringSize = [myString sizeWithFont:stringFont constrainedToSize:maximumSize lineBreakMode: UILineBreakModeWordWrap];
这将为您提供一个CGSize,然后您可以使用此处的高度并添加一点填充。您可以在此处找到更多信息:


Tim

使用
-sizeWithFont:constrainedToSize:lineBreakMode:
of
NSString
(您可以在此门户网站上找到更多信息)或使用包含每个
indexPath
高度的数组,如本例所示(SectionInfo类)