How can I fix this warning: setting the first responder view as a collection, but we don't know its type (cell / header / footer)?

I am trying to add a search bar to my collection view. I want to implement filtering data using a method - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

.

Everything worked fine when I used the table view. In the table view, when I added UISearchBar

with the method addSubView

, I got almost the same warning, but it disappeared when I added UISearchBar

with the method setTableHeaderView

.

But in the collection view, there is no method that does the same as the table view. So I added UISearchBar

in an additional header 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 the object UISearchBar

in this way, I get a warning "setting the first kind of collection view responder, but we don't know its type (cell / header / footer)?" every time i hit the search bar.

It seemed to work great apart from the warning, but I get into a problem when I type something. If I type one letter, the keyboard input view disappears, so I have to hit the search bar again to continue searching. If the word I want to find is 5 letter words, I have to hit the search bar 4 times. It's horrible.

I think this is due to a warning. I googled with the context of the warning and I found the only stackoverflow article related to this. (other articles are related to table view. They all seem to have been fixed with setTableHeaderView

.)

Article this .

But I have not registered the iOS developer program, so I cannot read this article. Are there any people willing to help me with this?

+3


source to share


2 answers


This is because the header view is reloaded when "reloadData" is called on the collection, so the search bar loses its first responder status. The only workaround I have been able to find is to keep the BOOL when there is an active search, and if so, then set the searchbar to firstResponder in the viewss collection of cellForItemAtIndexPath:



if(activeSearch && ![searchBar isFirstResponder])
{
    [searchBar becomeFirstResponder];
}

      

+2


source


This is  happening  because reloadData will also reloading the header. 

      

i.e. your old header is removed from the collection view, enqueued and reused during your data source implementation



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

0


source







All Articles