user1644365
user1644365

Reputation: 59

How to segue and pass data between uitableviews, when detail disclosure button is tapped?

First I need to clarify that when I don't use a detail disclosure button and just the disclosure indicator everything works perfectly.

After quite a research I am struggling with how to pass the correct data from one tableview to another, when the detail disclosure button is tapped. I am using core data to store my data and I have created the appropriate segue (Item Details Segue) from the master UITableViewController (SGProfileTVC) to the detail one SGItemDetailsTVC.

Here is my implementation of prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"Add Item Segue"])
{
    NSLog(@"Setting SGProfileTVC as a delegate of SGAddItemTVC");
    SGAddItemTVC *sgAddItemTVC = segue.destinationViewController;
    sgAddItemTVC.delegate = self;
    sgAddItemTVC.managedObjectContext = self.managedObjectContext;
} else if([segue.identifier isEqualToString:@"Item Details Segue"])
{
    NSLog(@"Setting SGProfileTVC as a delegate of SGItemDetailTVC");
    SGItemDetailTVC *sgItemDetailTVC = segue.destinationViewController;
    sgItemDetailTVC.delegate = self;
    sgItemDetailTVC.managedObjectContext = self.managedObjectContext;

    // Store selected item  in selectedItem property
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    self.selectedItem = [self.fetchedResultsController objectAtIndexPath:indexPath];

    NSLog(@"Passing the selected item (%@) to SGItemDetailTVC", self.selectedItem.name);
    sgItemDetailTVC.item = self.selectedItem;

} else {
    NSLog(@"Unidentified Segue");
}

}

I have also implemented the tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath method

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"Item Details Segue" sender:[self.tableView indexPathForSelectedRow]];
}

What is happening is that it segues normally, however when the new tableview loads it doesn't contain any data.

Any suggestions would be very much appreciated!!

Thank you!

Upvotes: 0

Views: 897

Answers (3)

Jeremy Brooks
Jeremy Brooks

Reputation: 589

Imotep has the correct answer. The code looks like this:

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

Upvotes: 0

Imotep
Imotep

Reputation: 2056

I guess that when you use an accessory button, the cell isn't selected. Instead of getting the indexPath with indexPathForSelectedRow, try to get the cell from the sender (which should be the cell itself or the accessory button, I can't test it right now). Once you have the cell, use indexPathForCell: to get its indexPath

Upvotes: 0

FrankZp
FrankZp

Reputation: 2042

Does your new Table View have a Fetched Results Controller? It should have one as the fetched results controller manages automatically the data display (e.g. reloads) of your table view.

Upvotes: 0

Related Questions