如何获取TableView中的单元格对象:(UITableView *)tableView heightForRowAtIndexPath函数?

我知道当返回heightForRowAtIndexPath时,单元格的内容。 在这个函数中,我如何获得单元格对象?

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

如果我尝试这个

  [tableView cellForRowAtIndexPath:indexPath] 

它失败,并在无限循环中熄灭。

任何想法?

调用self(委托)而不是tableView本身。

 id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 

使用func dequeueReusableCellWithIdentifier(identifier: String) -> AnyObject?UITableView
不要使用func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
可能的原因heightForRowAtIndexPathcellForRowAtIndexPath之前被调用,并且此方法使用索引path根据表格视图中单元格的位置执行额外的configuration。 但是既然现在还没有细胞,所以我认为是无限循环的。

heightForRowAtIndexPath:被调用时,单元格还没有被创build,所以没有办法“获取”单元格。 相反,看看你的模型,并确定单元格的高度。

如果你担心长string和需要更多的垂直空间,请考虑使用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]; 

它返回单元格对象。