Get the indexpath/row in XCODE 5?

I have an app that used to work before I upgraded to XCODE 5. I needed to find the row that was selected for a structure I was displaying which has over 100 rows so obviously can be scrolled on the display. The code that used to work is:


NSIndexPath *indexPath = [self.mainTableView indexPathForCell:myCell];

NSInteger row = [indexPath row];


Now, regardless of the row selected, the value of row is always 0. Anyone have any suggestions for me? Thanks.

Have you verified that self.mainTableView is not nil? Also, have you checked if the indexPath is nil?


How are you determining myCell?

I haven't checked for the values of self.mainTableView or indexPath. I'm not sure how to do that quite frankly.


I get myCell with this:


UITableViewCell *myCell = (UITableViewCell *)pressedButton.superview.superview;

You could, for example, do something like:

NSLog(@"mainTableView %@ indexPath%@", self.mainTableView, indexPath);


The way you get your cell view is not sustainable. The part of the view hierarchy controlled by UIKit can't be relied on to have a particular structure. See this question on StackOverflow, for example.

Well, I think you have identified my problem. Here is the result from the NSLog you suggested:


2015-07-19 20:42:26.328 elements1[4667:60b] mainTableView <UITableView: 0xa9ee800; frame = (0 34; 320 397); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x9250d20>; layer = <CALayer: 0x9272160>; contentOffset: {0, 0}> indexPath(null)


I'm guessing that Offset of "0,0" and "indexPath(null) is not good. I looked at the link you gave me and will need to study that in more detail to find the answer. It looks like someone else had the same issue. I'm pretty new at this and didn't quite understand what was posted but I'll try to figure it out. I started working on this app a few years ago and am just getting back to it now, trying to get it to work on XCODE 5.


Thanks for your help.

Follow-up question -


If I do as suggested in the post that was referenced above, which is


-(UITableViewCell*)GetCellFromTableView:(UITableView*)tableView Sender:(id)sender { CGPoint pos = [sender convertPoint:CGPointZero toView:tableView]; NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pos]; return [tableView cellForRowAtIndexPath:indexPath]; }


How would I then be able to get the actual row number for that cell? Ultimately, I need to do an update into that cell.

Well, that was only one of the approaches suggested. In any case, that method determines the index path on the way to finding the cell. So, you can just drop the last step and instead look at [indexPath row], just as you were trying to do before.

I'm clearly missing something here. I'm getting an "undeclared identifier" on GetCellFromTableView. Am I supposed to have that declared somewhere else?

Accepted Answer

No. That was just somebody's suggestion for a new method on your class. You don't even have to use that. You can just replace the two lines in your original post (wherever in your code they appear) with:

CGPoint pos = [pressedButton convertPoint:CGPointZero toView:self.mainTableView];
NSIndexPath *indexPath = [self.mainTableView indexPathForRowAtPoint:pos];
NSInteger row = [indexPath row];

Ken - THANK YOU! That took care of my problem. I was getting a bit discouraged thinking I had to start my project from scratch because my original design was no longer going to work with the updated software. Now you've fixed what would have been a show-stopper for me. I still have a few issues with sizing for the bigger screen displays, but so far I think I know how to fix those. Thanks very much for your patience and help.


Rick

Get the indexpath/row in XCODE 5?
 
 
Q