Indexpathforcell Returns Nil Since iOS7

indexPathForCell returns nil since ios7

Your approach to find the "enclosing" table view cell of a text field is fragile,
because is assumes a fixed view hierarchy (which seems to have changed between
iOS 6 and iOS 7).

One possible solution would be to traverse up in the view hierarchy until the table view cell is found:

UIView *view = textField;
while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
view = [view superview];
}
EditingCell *cell = (EditingCell *)view;

A completely different, but often used method is to "tag" the text field with the row
number:

cell.textField.tag = indexPath.row;   // in cellForRowAtIndexPath

and then just use that tag in the text field delegate methods.

tableView:indexPathForCell returns nil

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

IBAction always returns 0 of clicked index in iOS7

Below is what I used...

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:mainTableView];
NSIndexPath *indexPath = [mainTableView indexPathForRowAtPoint:buttonPosition];
NSLog(@"row index---%d", indexPath.row);

IBAction always returns 0 of clicked index in iOS7

Below is what I used...

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:mainTableView];
NSIndexPath *indexPath = [mainTableView indexPathForRowAtPoint:buttonPosition];
NSLog(@"row index---%d", indexPath.row);

tableView:indexPath* always returning a nil IndexPath

I'm making this an answer because it's resolved my problem.

I apologize for stringing everyone on with this question but it turns out the core issue was a failure to link the table view to it's IBOutlet in the view controller which I originally did but must of have accidentally removed when trying to implement this long press issue.

Adding the link back to the view controller so that self.tableView was no longer nil itself I began getting accurate results.

I apologize for failing to verify simple things like this before and will be mindful of them in the future. Thanks to @Joel and @rdelmar for attempting to get me on the right track but as you both said, each scenario should work (and does) so long as everything else is set up accordingly.

Get IndexPath.Row from TableView Objective C

Create an instance variable _lastClickedRow Set it with tableview delegate like below. And when you click the to "Get Row" button use _lastClickedRow.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

_lastClickedRow = indexPath.row;
}

- (IBAction)buttonGetNumber:(id)sender {

NSLog(@"%d" , _lastClickedRow);
}


Related Topics



Leave a reply



Submit