0

I have been following the answer of this question: How to update existing object in core data?

and in the answer it comes across this line of code to update a record within the array:

Favorits* favoritsGrabbed = [results objectAtIndex:0];  

Now this updates whatever is set a record 0 of the database, no matter what cell I select to edit.

I am sorry but I cannot figure out how to change it into updating the cell I have selected. Starting to grow grey hairs here:-)

The problem (maybe it is my mindset) is that I am required to give an integer and I am unable to find something to substitute it.

Any help would be great.

Community
  • 1
  • 1
jwknz
  • 6,598
  • 16
  • 72
  • 115

2 Answers2

0

Ummm… have you tried Favorits* favoritsGrabbed = [results objectAtIndex:indexPath.row]?


You need to pass favoritsGrabbed from the table view controller to the detail view controller.

Create a property in the detail view controller named favoritsGrabbed, then in the table view controller's -prepareForSegue:sender: set favoritsGrabbed.

The sender in -prepareForSegue:sender: should be a table view cell. So, from within the table view controller's -prepareForSegue:sender:, you should be able to do something like:

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
FavoritsDetailViewController *dest = segue.destination;
dest.favoritsGrabbed = [results objectAtIndex:indexPath.row];
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • yup, but that line is set in a detailViewController in a IBAction method - so there is nothing that declares the indexPath. I tried that as it seemed the most logical way to it. – jwknz Jul 02 '12 at 02:41
  • Cool I am playing with this now, and just because I am dumb about this, I am assuming that results is a newly created array on the detailViewController right? – jwknz Jul 02 '12 at 03:26
  • In this context, results is the array used to feed `-tableView:cellForRowAtIndexPath:`. Remember, the logic I provided is happening in the table view controller, not the detail view controller. In the detail view controller, you should only need to use the property `favoritsGrabbed`. After this change, results should not be needed in the detail view controller at all. – Jeffery Thomas Jul 02 '12 at 03:55
  • okay, so if that is the case how do I display those results then? if I want to display the textLabel of a TableViewCell in a textField on the detailViewController? - I have been trying to link them together. – jwknz Jul 02 '12 at 03:58
  • Use the favoritsGrabbed property that you will create in the detail view controller. To be concise, the table view controller sets the core data object for the detail view controller instead of the detail view controller fetching it. – Jeffery Thomas Jul 02 '12 at 04:57
0

I'm assuming you mean a table view cell? Have you connected the table to an IBOutlet, where you can reference the selected cell via the tableView's indexPathForSelectedRow? Can we see some code?

Gobot
  • 2,464
  • 1
  • 21
  • 26