tableView:indexPathForCell returns nil

tacos_tacos_tacos picture tacos_tacos_tacos · Aug 3, 2011 · Viewed 19.4k times · Source

I am using the method tableView:indexPathForCell to implement a custom delegate that can dynamically resize a UITableViewCell based on the frame size of the UIWebView that is inside of it. The problem is that tableView:indexPathForCell is returning nil when I try to find out what the indexPath of a particular cell is:

- (void)trialSummaryTableViewCell:(TrialSummaryTableViewCell *)cell shouldAssignHeight:(CGFloat)newHeight {
    NSIndexPath *indexPath = [tableV indexPathForCell:cell];
    NSLog(@"tableV: %@\ncell: %@\nindexPath: %@", tableV, cell, indexPath); //<-- 
    // ...
}

Here, tableV does not return nil, cell does not return nil, but indexPath returns nil.

What am I doing wrong?

Edit: I am calling -(void)trialSummaryTableViewCell from the tableView:cellForRowAtIndexPath: method

Answer

Jorge Perez picture Jorge Perez · Aug 10, 2012

It could be that the cell is not visible at this moment. tableView:indexPathForCell returns nil in this situation. I solved this using indexPathForRowAtPoint this method works even if the cell is not visible. The code:

UITableViewCell *cell = textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center];