UISearchController in UITableViewController disappears

I'm using NSFetchedResultsController for the tableview display the results returned by Core Data.

Use UISearchController to search. I have the following problem: sometimes, if I search while the tableView is being updated, the search bar disappears.

The table view is in a navigation controller.


- (void)viewDidLoad {
    [super viewDidLoad];
     ...

    [self.view setMultipleTouchEnabled:NO];
    [self.tableView setMultipleTouchEnabled:NO];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 44, 0)];

    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO; /
    self.searchController.hidesNavigationBarDuringPresentation = NO;

    [self.searchController.searchBar sizeToFit];
    self.searchController.searchBar.delegate = self; /
    self.definesPresentationContext = YES;

    self.tableView.tableHeaderView = self.searchController.searchBar;
     [self fetchedResultsController];
     ...
}



Any idea what may be wrong? Thanks.

I don't know, but Apple's Mail app does the same thing (search bar goes blank sometimes) so you're not alone.

It seems that the problem lies in the beginUpdates and methods endUpdates. These methods interfere in any way in the animation of the search bar. I would like to understand the cause.


- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

[self.tableView beginUpdates];

}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

UITableView *tableView = self.tableView;

switch(type) {

case NSFetchedResultsChangeInsert:

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop];

break;

case NSFetchedResultsChangeDelete:

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];

break;

case NSFetchedResultsChangeUpdate:

[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

break;

case NSFetchedResultsChangeMove:

[tableView deleteRowsAtIndexPaths:[NSArray

arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

[tableView insertRowsAtIndexPaths:[NSArray

arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

break;

}

}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

[self.tableView endUpdates];

}



To solve the problem I deleted the above code and I used [self.tableView reloadData];


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

[self.tableView reloadData];

}

Had a similar issue with a plain UISearchController. I was updating the UITableView (with begin/endUpdates) during updateSearchResults. It seems if this is done while the search controller is animating we experience the visual glitch.


Note that this occurs as soon as begin/endUpdates is used - regardless of whether or not the animation block contains any row animations. I fixed it by tweaking my code to avoid empty begin/endUpdates blocks.

UISearchController in UITableViewController disappears
 
 
Q