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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5639366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:33:38  来源:igfitidea点击:

How to get the cell object in tableView:(UITableView *)tableView heightForRowAtIndexPath function?

iphoneobjective-ciosipad

提问by user348398

I wand to know the content of the cell when returning the heightForRowAtIndexPath. In this function how do I get the cell object?

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

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

}

If I try this

如果我试试这个

 [tableView cellForRowAtIndexPath:indexPath]

it fails and goes off in an infinite loop.

它失败并无限循环。

Any idea?

任何的想法?

回答by NWCoder

Call self (the delegate) rather than the tableView itself.

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

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

回答by Puneet Sharma

Use: func dequeueReusableCellWithIdentifier(identifier: String) -> AnyObject?of UITableView.
Don't use: func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
Possible Reason: heightForRowAtIndexPathgets called before cellForRowAtIndexPathand this method uses the index path to perform additional configuration based on the cell's position in the table view. But since, there is still no cell yet, hence it goes to infinite loop I think.

使用func dequeueReusableCellWithIdentifier(identifier: String) -> AnyObject?UITableView
不要使用func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
可能的原因heightForRowAtIndexPath之前被调用,cellForRowAtIndexPath并且此方法使用索引路径根据单元格在表格视图中的位置执行其他配置。但是因为仍然没有单元格,因此我认为它进入了无限循环。

回答by colby

The cell hasn't been created when heightForRowAtIndexPath:is called, so there's no way to "get" a cell. Instead, look at your model and determine the height of the cell that way.

heightForRowAtIndexPath:调用时尚未创建单元格,因此无法“获取”单元格。相反,请查看您的模型并以这种方式确定单元格的高度。

If you're concerned about long strings and needing more vertical space in your cell, consider using sizeWithFont:forWidth:lineBreakMode:. Docs here: https://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

如果您担心长字符串并且需要更多的单元格垂直空间,请考虑使用sizeWithFont:forWidth:lineBreakMode:. 文档在这里:https: //developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

回答by onnoweb

I don't think you can or you should. You'll need to determine from indexPath which data you will be displaying in the corresponding cell and thus calculate the needed height of the cell.

我认为你不能,也不应该。您需要从 indexPath 确定您将在相应单元格中显示哪些数据,从而计算所需的单元格高度。

So you'll need to write some code to get from indexPath to the right data in your data model.

因此,您需要编写一些代码来从 indexPath 获取数据模型中的正确数据。

回答by Tendulkar

uitableviewcell *ce=[self.view viewwithtag:indexPath.row]; 

it returns the cell object.

它返回单元格对象。