3

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?,

Community
  • 1
  • 1
glast
  • 383
  • 1
  • 4
  • 17

2 Answers2

2

This happens because the header view is reloaded when "reloadData" is called on the collectionview, thus the searchbar looses it's first responder status. The only workaround I could find is to keep a BOOL of when an active search is going on and if it is, then set the searchbar to firstResponder in the collectionviews "cellForItemAtIndexPath":

if(activeSearch && ![searchBar isFirstResponder])
{
    [searchBar becomeFirstResponder];
}
Jason Shehane
  • 413
  • 4
  • 6
0
This is  happening  because reloadData will also reloading the header. 

i.e. your old header is removed from the collection view, queued and reused at the time of your dataSource implementation

don't worry about this warning. you can ignore this warning

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • Then how can I fix the problem that if I type one letter, the keyboard input automatically goes down? When I added search bar directly to the view, that problem didn't occur but in that way I can't use layout what I want to... – glast Jan 26 '13 at 16:25
  • Based on your answer, I guess the problem occurs because of reloadData in textDidChange. I guess when I type a letter, the header view contains search bar dequeued and queued, so the keyboard input goes down... – glast Jan 26 '13 at 16:33
  • Is there any other way to include search bar in collection view, so that the search bar goes up and finally hides as I scroll down? – glast Jan 26 '13 at 16:35
  • There might be a way around this, but so far I haven't looked into this – Ravindra Bagale Jan 26 '13 at 16:38
  • I solved the problem [this way](http://stackoverflow.com/questions/14540291/how-to-reload-only-data-section-of-uicollectionview). I found that there is just one difference between this way and setting the `tableHeaderView`, which is that the scroll bar goes behind the search bar. – glast Feb 01 '13 at 12:35