Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Diary App Using Core Data Deleting and Updating Data Updating Data

Richard Hong
Richard Hong
2,951 Points

IndexPath question

I tested this with my code, and can't seem to understand why indexpath is different when using the following two methods:

UITableViewCell *cell = sender; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

The 1st code works, but the second will return unrecongized selector error, which I do not understand why.

Thank you!

4 Answers

Stone Preston
Stone Preston
42,016 Points

in your first block, the segue.destinationViewCOntroller is actually the navigationController, which is why you are getting that error. the navigationController does not have an entry property. you need to access the topViewController of the segue.desitinationViewController and set that to be the REntryViewController evc.

UINavigationController *navigationController = segue.destinationViewController; 
REntryViewController *evc = (REntryViewController *)navigationController.topViewController;
evc.entry = [self.fetchedRequestController objectAtIndexPath:indexPath];

it looks like you did it c orrectly in the second block, but not hte first block of code

Richard Hong
Richard Hong
2,951 Points

Thanks! totally forgot that it is going to the navigation controller instead!

Stone Preston
Stone Preston
42,016 Points

hmm the second one seems like it should work to me. can you post the actual error text you get in the console?

Richard Hong
Richard Hong
2,951 Points

Sure thing!

I should have posted the entire block since I am using it a little bit differently:

NSIndexPath indexPath = [self.tableView indexPathForSelectedRow]; REntryViewController *evc = (REntryViewController) segue.destinationViewController; evc.entry = [self.fetchedRequestController objectAtIndexPath:indexPath];

vs.

UITableViewCell *cell = sender; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; UINavigationController *navigationController = segue.destinationViewController; REntryViewController *entryViewController = (REntryViewController *)navigationController.topViewController;

Console (For the first block): [UINavigationController setEntry:]: unrecognized selector sent to instance 0x8c7ed20 //evc = (UINavigationController*) 0x8c7ed20

Thank you so much!!

Stone Preston
Stone Preston
42,016 Points

its not this line causing the issue

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

its this one

 evc.entry = [self.fetchedRequestController objectAtIndexPath:indexPath];

are you sure your REntryViewController class has an entry property?

Richard Hong
Richard Hong
2,951 Points

Yup absolutely. I know it has an entry property because

  1. I can see it in the file
  2. It works with the second block of code (I forgot to add the following code to second block of code I posted earlier)

entryViewController.entry = [self.fetchedResultsController objectAtIndexPath:indexPath];