Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reloading UITableView behind UISearchDisplayController

I've run into this really strange phenomenon that I can't quite figure out. I have a UITableViewController that manages a UITableView. Pretty simple. I also have a UISearchDisplayController for searching the contents of the table view. The searching functionality will be able to delete items of the content displayed by the table view. So if the user chooses to delete one of the items they found while searching, I want to not only reload the UISearchDisplayController's table view but also the UITableViewController's table view. When I do that, the sections of the regular table view pop out and display above the UISearchDisplayController. It's really quite strange. I think the best way to explain it is with an image:

http://img46.imageshack.us/img46/2818/screenshot20100905at140.png

If any of you know what could possibly be causing this problem or know a workaround, that would be fantastic.

like image 642
Ethan Vaughan Avatar asked Sep 05 '10 18:09

Ethan Vaughan


2 Answers

UPDATED AGAIN

As it turns out, if a table's header is reloaded in the background it pops in front of the search controller no matter what.

I solved this by disabling the fetchedResultsController (setting it to nil) and letting it load lazily again when needed when the search disappears.

UPDATED - Original answer below

In my case I'm using two fetchedResultsControllers one for the main tableview and one for the search.

I discovered that preventing animations when adding the section headers prevents this bug. So while searchDisplayController.active I simply disable the animation of the section change. see code below.

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    if (!self.reordering) {
        UITableView *myTableView = controller == __fetchedResultsController ? self.tableView : self.searchDisplayController.searchResultsTableView;
        UITableViewRowAnimation animation;
        if (self.searchDisplayController.active) {
            animation = UITableViewRowAnimationNone;
        } else {
            animation = UITableViewRowAnimationFade;
        }

        switch(type)
        {
            case NSFetchedResultsChangeInsert:
                [myTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:animation];
                break;

            case NSFetchedResultsChangeDelete:
                [myTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:animation];
                break;
        }
    }
}

ORIGINAL ANSWER

The other answer doesn't actually work on it's own. The reason is, the header that is showing is not a header in the searchDisplayController's tableview. It's a header from the main tableview that for some reason is being added above the search table view in the view hierarchy.

I solved this problem by disabling updates to the main tableview while searchDisplayController.active = YES.

In my case I'm using a lazily loaded fetched results controller so I did it like this:

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
    [self.tableView reloadData];
}

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    self.fetchedResultsController.delegate = nil;
    self.fetchedResultsController = nil;
}

However, I still have the problem that if I want to reloadData on the main tableview so it is seen in the background, the section headers still float in front of the darkened area.

Does anyone have a better solution for this? It seems like a legitimate bug for viewForHeaderInSection and titleForHeaderInSection when data is reloaded while covered by a UISearchDisplayController.

The simplest answer for me is to try and override the search view so you can't see the background table. But that takes away from the "Appleness" of the app.

like image 162
Derrek Avatar answered Sep 22 '22 05:09

Derrek


Our solution is to do the following. It has only been tested in iOS 7:

  1. In viewForHeaderInSection, return nil if self.searchDisplayController.active is YES
  2. In didHideSearchResultsTableView, call [self.tableView reloadData] to reload the headers when the search table disappears
like image 36
EricS Avatar answered Sep 22 '22 05:09

EricS