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

30,077

Solution 1

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

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

Solution 2

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.

Solution 3

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

Share:
30,077

Related videos on Youtube

user348398
Author by

user348398

Updated on April 28, 2020

Comments

  • user348398
    user348398 almost 4 years

    I wand to know the content of the cell 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?

  • NWCoder
    NWCoder almost 13 years
    if your cellForRowAtIndexPath is using the heightForRowAtIndexPath then there will always be infinite recursion. If so, you'll have to architect this differently.
  • Joe
    Joe almost 13 years
    Not a recommended way of doing it. The heightForRowAtIndexPath gets called before cellForRowAtIndexPath for a reason, you should be able to calculate the height based on the data you have. It is also not recommended to use heightForRowAtIndexPath unless you really need to have different heights for the cells as it is much slower as the data set increases in size.
  • XJones
    XJones almost 13 years
    this is wrong, you can send tableView:cellForRowWithIndexPath in this case. @user... is just sending it to the wrong object.
  • XJones
    XJones almost 13 years
    @Joe: semi-disagree. If you can easily calc height w/o needing the cell then that's fine. There are legitimate cases where you need to instantiate the cell to know it's height. I have many custom cell classes that are complex enough that calculating height w/o laying out the subviews is difficult. Some of the Apple examples do this as well.
  • NWCoder
    NWCoder almost 13 years
    This would only work is the cells are created with this tag. Also note this may be problematic with reused cells, because they may be dequeued by the time you look for it.
  • JP Illanes
    JP Illanes about 10 years
    It seems that if you use dequeueReusableCellWithIdentifier:forIndexPath: for reusing cells, tableView:heightForRowAtIndexPath: will be called while initing the cell, and you will get an infinite recursion. So this method won't work in that case.
  • jose920405
    jose920405 about 8 years
    Works for me, but you can feel the damage it does to the performance When try to add new cells in the tableview. Likewise it is not advisable to access a cell in heightForRowAtIndexPath