tableView:indexPathForCell returns nil

北慕城南 提交于 2019-11-30 01:18:10

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];

[tableV indexPathForCell:cell] returns nil if cell is not visible.

Also if you are calling "trialSummaryTableViewCell" from the "cellForRowAtIndexPath" method, you could easily pass indexPath also to the "trialSummaryTableViewCell" method.

A small update, since Jorge Perez' answer will fail starting at iOS7 (since a UIScrollView has been inserted and calling textField.superview.superview won't work anymore).

You can retrieve the NSIndexPath like this:

//find the UITableViewCell superview
UIView *cell = textField;
while (cell && ![cell isKindOfClass:[UITableViewCell class]])
    cell = cell.superview;

//use the UITableViewCell superview to get the NSIndexPath
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!