首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在UICollectionViewController中使用UISearchDisplayController?

如何在UICollectionViewController中使用UISearchDisplayController?
EN

Stack Overflow用户
提问于 2012-09-28 01:27:42
回答 3查看 6.8K关注 0票数 16

如果您使用的是带有UITableViewController的UISearchDisplayController,当用户点击搜索栏时,搜索栏的动画效果会取代导航栏。

我希望在UICollectionViewController顶部使用UISearchBar时也能获得同样的效果。有什么想法吗?

EN

回答 3

Stack Overflow用户

发布于 2012-12-06 13:08:07

我必须以编程方式将searchBar添加为UICollectionReusableView的子视图,因为在分配插座时,我始终无法通过IB使其工作,因为我一直收到原型错误。在实现文件中添加搜索栏对我来说很有效。

相关方法如下。

代码语言:javascript
复制
-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    _objectChanges = [NSMutableArray array];
    _sectionChanges = [NSMutableArray array];
    [self performFetch];
    searchBar = [[UISearchBar alloc]
                  initWithFrame:CGRectMake(0.0, 50.0, self.view.bounds.size.width,
                                           44.0)];
    searchBar.placeholder = @"Search for channels";
    searchBar.tintColor = [UIColor blackColor];
    searchBar.delegate = self;
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SupplementaryView *header = nil;

    if ([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                    withReuseIdentifier:@"reuseHeader"
                                                           forIndexPath:indexPath];

        [header addSubview:searchBar];

    }
    return header;
}
票数 2
EN

Stack Overflow用户

发布于 2012-12-08 12:35:19

我刚刚收到了同样的询问,我想出了一个半生不熟但有效的解决方案,不涉及重写UISearchDisplayController。

(最终结果:一个UITableView --它响应于shouldReloadTableForSearchString--覆盖在UICollectionView之上,一旦单击search,它就会消失,结果就会显示在collectionView中。如果感兴趣,请继续阅读)

在IB中创建了一个我插入其中的UIViewController (见屏幕截图):一个用于布局目的的视图-->其中我首先放入了一个UISearchBar和显示控制器。在同一个视图中(并排),我放入了一个带有自定义UICollectionViewCell的UICollectionView。然后我放了一个UITableViewProvider (有一个自定义的UITableCell,但这不是必需的,你也可以忽略屏幕截图中的UITabBarItem和导航项目,这是无关紧要的)

我将UITableView的高度设置为0,并连接了所有的出口和代理,最终结果如下所示,当光标进入UISearchBox时,UITableView覆盖在UICollectionView上,因为输入的是shouldReloadTableForSearchString del并将结果显示在tableView中;在searchBarSearchButtonClicked上,我只需设置UICollectionView的dataSource并在它的outlet上调用reloadData。

有人会认为苹果应该推广搜索显示控制器;也许在未来的版本中?

(这是因为这是优化UISearchDisplay控制器的方式,IIRC实际上每个字符条目都有一个UITableView堆叠在一起)

(没有发布代码,因为其中涉及了相当多的内容;如果有什么不直接的地方,请询问)

票数 1
EN

Stack Overflow用户

发布于 2015-11-30 21:19:31

这里只有一个快速的代码-对我有效!

代码语言:javascript
复制
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    let headerView = self.mainPictureCollectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "headerView", forIndexPath: indexPath)

    headerView.addSubview(searchBar)
    return headerView
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12626913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档