I have a UICollectionView
that have section header. In the section header I have a UISearchBar
. I want to filter the content in my collection view when I type in the search bar. I do this with the following method:
// The method to change the predicate of the FRC
- (void)filterContentForSearchText:(NSString*)searchText
{
NSString *query = searchText;
if (query && query.length) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@ or createdAt contains[cd] %@ or noteText contains[cd] %@ or keywords contains[cd] %@", query, query, query, query];
[self.fetchedResultsController.fetchRequest setPredicate:predicate];
} else {
[self.fetchedResultsController.fetchRequest setPredicate:nil];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Error");
}
[self.collectionView reloadData];
}
This method gets called every time the search bar text changes. The line [self.collectionView reloadData]
hided the keyboard for every character. Is it possible to reload only the data in a UICollection view and not reload the Supplementary views like section headers headers?
The data in my collectionView comes from a NSFetchResultController.
I'm pretty happy with how my UI works so if there is a simple way to NOT reload the section header, then that would be great!
I finally figured this out after trying many different options. The solution is to make your header of its own section so you can reload the other sections independently without reloading the one your header is attached to.
So:
Then, we you need to reload your data:
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
Your searchbar or text fields should retain their focus while the user is typing and you can update the results in the background.
did you tried with one of this options?
To reload just the new items
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
To reload the complete section
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
Or, if it doesn't work.. make searchBar first responder again after your updates
[self.collectionViewPackages performBatchUpdates:^{
[self.collectionView reloadData];
} completion:^(BOOL finished) {
[self.searchBar becomeFirstResponder];
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With