Reputation: 119
I have a table view controller and a view controller. In the table view controller you can add contacts and then it takes you to the view controller and there you can set the person's name, email, and phone. It saves the data with Core Data. Before, I made it so that when you press the table view cell, it took you to the same view controller but the fields were already filled in with the contact's information and it could be edited. Then these were saved and the contact's new info would be updated. Now, I changed it so there is a detail disclosure button and when that is pressed, it is supposed to have the same function as tapping the cell did before. If I press the detail disclosure button, the edit screen for the first object in the tableView pops up, even if I select the third object. On the simulator, if I click the cell, the cell simply highlights itself as it should and has no other function. When the cell is selected and the detail disclosure button is clicked, it goes to the edit screen of the correct object.
For example; If I click the detail disclosure button of the fourth object in the tableView, it takes me to the edit screen of the first object. If I click the cell and while that cell is selected, click the detail disclosure button, it takes me to the edit screen of the fourth object.
Function where detail disclosure is being called:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"UpdateContacts"]) {
NSManagedObject *selectedDevice = [self.contactarray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
MyDetailViewController *destViewController = segue.destinationViewController;
destViewController.contactdb = selectedDevice;
}
}
Accessory Button Tapped For Row With Index Path
NSInteger selectedRow;
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { selectedRow = indexPath.row;
[self performSegueWithIdentifier:@"UpdateContacts" sender:self]; }
viewDidAppear (contains FetchRequest)
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Contacts"]; self.contactarray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
UPDATE
Now, when the detail disclosure is selected, it goes to the correct one. If you click another detail disclosure, it goes to the same one until it is closed out and done again. For example, if I open up the app and tap the detail disclosure for the third object, it goes to the edit screen for the third object. If I go back to the main menu screen and click on the second object, it takes me to the edit screen for the third object. If I go back to the main menu screen and click the second one again, it will correctly take me to the second object's edit screen.
Thanks in advance!
Upvotes: 0
Views: 519
Reputation: 119
Fixed the problem; just changed indexPathForSelectedRow in
NSManagedObject *selectedDevice = [self.contactarray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
to indexPathForCell:sender
Thank you for the help Bernhard, you helped me figure out where the problem was.
Upvotes: 0
Reputation: 469
This looks like you're referencing the index path of only a selected row:
NSManagedObject *selectedDevice = [self.contactarray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
You can add a property for the indexPath, set it in "accessoryButtonTappedForRowWithIndexPath:" and use it instead of [self.tableView indexPathForSelectedRow].
In your header file create a property of type NSIndexPath. Looks like this:
@property (strong, nonatomic) NSIndexPath *yourIndexPath;
In the "accessoryButtonTappedForRowWithIndexPath:" method you set the indexPath:
_yourIndexPath = indexPath;
In your segue your reference the array like this:
NSManagedObject *selectedDevice = [self.contactarray objectAtIndex:[_yourIndexPath.row]];
Edit: Depending how you segue and how you have your datasource set up, you can also fish out the index path from prepareForSegue and get the NSManagedObject from your fetched results controller / array:
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSManagedObject *object = [[self fetchedResultsController]objectAtIndexPath:indexPath];
This works flawlessly for me. If you still run into problems, it's not that.
Upvotes: 0