I'm trying to adding search bar to my collection view. What I want to implement is filtering data using - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
method.
Everything worked fine when I used table view. In table view, when I added UISearchBar
using addSubView
method, I got the almost same warning, but it disappeared when I added UISearchBar
using setTableHeaderView
method.
But in collection view, the method which performs same as table view doesn't exist. So I added UISearchBar
to supplementary header view, like below:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView;
if (kind == UICollectionElementKindSectionHeader) {
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
[reusableView addSubview:self.searchBar];
}
return reusableView;
}
When I added UISearchBar
object by this way, I get the warning, "setting the first responder view of the collection view but we don't know its type (cell/header/footer)?" every time I tap the search bar.
It seemed to work fine except the warning, but I get into the problem when I type something. If I type one letter, the keyboard input view goes down, so I have to tap the search bar again to keep searching. If the word I want to search is 5 letter word, I have to tap the search bar 4 times. It is awful.
I think this occurs because of the warning. I googled with the warning context, and I found the only article on stackoverflow related to this.
(other articles are related with table view. All of them seems to be fixed by using setTableHeaderView
.)
The article is this.
But I don't registered iOS developer program, so I can't read that article. Is there anybody willing to help me with this?,