当返回heightForRowAtIndexPath时,我想知道单元格的内容。在这个函数中,如何获取单元格对象?
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
}
如果我尝试这个
[tableView cellForRowAtIndexPath:indexPath]
它失败了,并以无限循环的方式运行。
知道吗?
调用self(委托(而不是tableView本身。
id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
使用:UITableView
的func dequeueReusableCellWithIdentifier(identifier: String) -> AnyObject?
不要使用:func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
可能原因:heightForRowAtIndexPath
在cellForRowAtIndexPath
之前被调用,此方法使用索引路径根据单元格在表视图中的位置执行额外配置。但由于,还没有细胞,所以我认为它进入了无限循环。
调用heightForRowAtIndexPath:
时尚未创建单元格,因此无法"获取"单元格。相反,您可以查看模型并通过这种方式确定单元的高度。
如果您担心长字符串,并且在单元格中需要更多的垂直空间,请考虑使用sizeWithFont:forWidth:lineBreakMode:
。此处的文档:https://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
我认为你不能也不应该。您需要根据indexPath确定将在相应单元格中显示哪些数据,从而计算所需的单元格高度。
因此,您需要编写一些代码来从indexPath获取数据模型中的正确数据。
uitableviewcell *ce=[self.view viewwithtag:indexPath.row];
它返回单元格对象。