19

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?

codercat
  • 22,873
  • 9
  • 61
  • 85
user348398
  • 511
  • 2
  • 7
  • 17

5 Answers5

27

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

id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
NWCoder
  • 5,296
  • 1
  • 26
  • 23
  • 2
    if your cellForRowAtIndexPath is using the heightForRowAtIndexPath then there will always be infinite recursion. If so, you'll have to architect this differently. – NWCoder Apr 12 '11 at 17:47
  • 4
    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. – Joe Apr 12 '11 at 17:50
  • 4
    @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. – XJones Apr 12 '11 at 17:59
  • 4
    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. – JP Illanes Mar 19 '14 at 09:44
  • 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` – jose920405 Feb 18 '16 at 19:15
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.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
1

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

innesngunn
  • 331
  • 1
  • 8
0

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.

onnoweb
  • 3,038
  • 22
  • 29
  • 1
    this is wrong, you can send `tableView:cellForRowWithIndexPath` in this case. @user... is just sending it to the wrong object. – XJones Apr 12 '11 at 17:57
-4
uitableviewcell *ce=[self.view viewwithtag:indexPath.row]; 

it returns the cell object.

Floern
  • 33,559
  • 24
  • 104
  • 119
Tendulkar
  • 5,550
  • 2
  • 27
  • 53
  • 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. – NWCoder Apr 12 '11 at 21:11