0

So I've got a button on my UITableView that turns on and off a filter, that filters by a BOOL called isLiked. I can display all results, or just the ones designated as liked. I can tap on any cell and go into a detail page where I can flip that BOOL on or off.

If I'm in the liked-fitered list and I tap one, then turn its favourite status to off, then go Back to the liked-filtered once more, it hasn't disappeared. If I flip the filter off and on again, that entry disappears.

I'd like that change to occur as soon as I come back out of that view, rather than needing to turn the filter on and off for it to take effect. How can I achieve that? Some relevant code is below:

Here's the method that is called when I turn the filter on and off:

- (IBAction) filterLiked: (id) sender
{
    if (isDisplayingLiked) {
        // Revert to the predicate that only removes disliked entries.
        [_fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"isDisliked == 0"]];
        NSError *error;
        if (![self.fetchedResultsController performFetch: &error]) NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        // Update the tableView and update state variables.
        [self.tableView reloadData];
        isDisplayingLiked = NO;
        [_showLikedButton setTitle: @"Liked"];

    } else {
        // Revert to the predicate that only shows liked entries.
        [_fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"isLiked == 1"]];
        NSError *error;
        if (![self.fetchedResultsController performFetch: &error]) NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        // Update the tableView and update state variables.
        [self.tableView reloadData];
        isDisplayingLiked = YES;
        [_showLikedButton setTitle: @"All"];
    }
}

Here's the code that's called when I turn the Liked status on or off from within a detail view:

- (IBAction) changeLikedSwitch: (id) sender
{
    UISwitch *likedSwitch = (UISwitch *) sender;
    if ([likedSwitch isOn]) {
        [_selectedQuote setIsLiked: [NSNumber numberWithBool: YES]];
    } else {
        [_selectedQuote setIsLiked: [NSNumber numberWithBool: NO]];
    }

    NSError *error;
    if (![[[CDManager sharedManager] managedObjectContext] save:&error]) NSLog(@"Saving changes failed: %@, %@", error, [error userInfo]);
}

If you need any more code, please let me know.

Luke
  • 9,512
  • 15
  • 82
  • 146

1 Answers1

0

You need to reload the data of your table view as soon as you return from the detail view.

One common technique is to use Delegation. Make the table view controller the delegate of the detail view controller. Then, in the detail view controllers viewWillDisappear: you could call the delegate's reloadData method.

ilmiacs
  • 2,566
  • 15
  • 20
  • I've tried adding a delegate, but calling `reloadData` on the table at that point has no effect at all. I still have to go and turn the filter on and off to get the changes to reflect. – Luke Feb 26 '13 at 11:51
  • I'm also getting crashes when coming out of the view after unliking some objects, I think it's expecting them to still be there. Hugely confused. – Luke Feb 26 '13 at 11:55
  • Sorry for having confused you. Do you use the `UINavigationController` mechanism or do you present the detail view modally? – ilmiacs Feb 26 '13 at 11:59