How to filter UICollectionView and keep keyboard up?

Mike Katz picture Mike Katz · Mar 6, 2013 · Viewed 9k times · Source

I've added a UISearchBar to a UICollectionView and in the delegate searchBar:textDidChange: filter my model and call [collectionView reloadData]. reloadData (as well as reloadSection, etc) wants to take away firstResponder from the searchbar's textfield, thus dismissing the keyboard.

I am trying to build a "live updating" filter and so it's annoying to have the keyboard go away after each character typed.

Any ideas?

Answer

Tran Trung Hieu picture Tran Trung Hieu · Mar 17, 2015

In searchBar delegate function , I use performBatchUpdates, first,reload collectionView then call [self.searchBar becomeFirstResponder] to display keyboard

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [self setEditing:NO animated:YES];
    [searchBar setShowsCancelButton:YES animated:YES];

        [self.collectionView performBatchUpdates:^{
            [self.collectionView reloadData];
        } completion:^(BOOL finished) {
            [self.searchBar becomeFirstResponder];
        }];
}