UISearchBar in UICollectionReusableView?

John Shockey john at johncshockey.com
Tue Oct 8 09:28:29 EDT 2013


Susan,

I haven't looked very far into your code. But

  kind == UICollectionElementKindSectionHeader

seems questionable. This require the strings to be the exact same string object. I think you should compare for string equality. (isEqualToString:)

That probably isn't the problem, but you might want to fix that and try again, just in case.

John



On Oct 8, 2013, at 8:42 AM, Susan Cudmore <susan.cudmore at gmail.com> wrote:

> 
> Hi All,
> 
> I wrote the following on the Apple Dev forum this morning, but I don't have high hopes of getting a response so I thought I'd shoot it out here. I'm trying to figure out if it is my bug or Apple's bug that is giving me trouble. 
> 
> Thanks for any insight!
> 
> /Sue
> 
> 
> 
>  
> 
> 
> Trouble adding UISearchBar to UICollectionReusableView header
> 
> I would like to add a UISearchBar to the header of a UICollectionView so that I can hide the searchBar off screen (with a content offset) until the user pulls it into view and I also want the bouncing behavior that comes with collection view. 
> 
> I'm having trouble accessing the UISearchDisplayController and setting it's searchBar to the one I created.
> 
> I've tried creating the UISearchBar programatically in my VC that is delegate for the UISearchDisplayController and the UISearchBar. I've also tried adding the UISearchBar to the reusable header view in IB.
> 
>  
> I had it mostly working with a programatic creation of the searchBar in - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath   except that the reusable view seemed to "cover" the searchBar at various places as I scrolled it on and off the screen. And sometimes the Cancel method would not fire. I think the last problem may have been due to a different than expected searchBar being assigned to the UISearchDisplayController.
> 
>  
> I've also tried holding onto a programmatically created UISearchDisplayController (previous posts point to retaining it in this manner).
> 
>  
> 
> Has anyone had success adding a UISearchBar to a collection view? Is what I'm trying to do technically impossible even though it works for UITableView?
> 
> 
> 
> - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
> 
>  
> Here's my sample code that works except that the searchBar gets clipped from the top in various locations as the reusable view is scrolled on and off screen. And, sometimes the cancel button fires and sometimes it doesn't after the reusable view is freshly dequeued.
> 
>  
> 
> 
> - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
> 
> 
> 
> {
> 
>     UICollectionReusableView* reusableView;
> 
>  
>     if (kind == UICollectionElementKindSectionHeader) {
> 
>  
>         UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
> 
>  
>         if (!self.searchBar)
> 
>         {
> 
>             self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
> 
>         }
> 
>         [self configureSearchBar];
> 
>         [headerView addSubview:self.searchBar];
> 
>         [self pinSearchBarto:headerView];
> 
>  
>         if (!self.searchController)
> 
>         {
> 
>             self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
> 
>         }
> 
>         self.searchController.searchResultsDataSource = self;
> 
>         self.searchController.searchResultsDelegate = self;
> 
>         self.searchController.delegate = self;
> 
>         reusableView = headerView;
> 
>     }
> 
>     return reusableView;
> 
> 
> }
> 
> _______________________________________________
> MacTechGroup-discuss mailing list
> MacTechGroup-discuss at lists.mactechgroup.org
> http://lists.mactechgroup.org/mailman/listinfo/mactechgroup-discuss



More information about the MacTechGroup-discuss mailing list