开发者

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

I wand to know the content of the cell开发者_如何学JAVA when returning the heightForRowAtIndexPath. In this function how do I get the cell object?

- (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?


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

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


Use: func dequeueReusableCellWithIdentifier(identifier: String) -> AnyObject? of UITableView.
Don't use: func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
Possible Reason: heightForRowAtIndexPath gets called before cellForRowAtIndexPath and 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.


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.

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


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.

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


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

it returns the cell object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜