简体   繁体   English

如何将UISearchDisplayController与UICollectionViewController一起使用?

[英]How can I use UISearchDisplayController with UICollectionViewController?

If you use a UISearchDisplayController with a UITableViewController, when the user taps the search bar it animates up to replace the nav bar. 如果您使用带有UITableViewController的UISearchDisplayController,当用户点击搜索栏时,它会动画以更换导航栏。

I'd like to get that same effect when using a UISearchBar at the top of a UICollectionViewController. 我想在UICollectionViewController顶部使用UISearchBar时获得相同的效果。 Any ideas? 有任何想法吗?

I had to add the searchBar programmatically as a subview of the UICollectionReusableView, I could never getting it working through IB as I kept getting prototype error when assigning an outlet. 我不得不以编程方式将searchBar添加为UICollectionReusableView的子视图,我无法通过IB工作,因为我在分配插座时一直遇到原型错误。 Adding the search bar in the implementation file worked for me. 在实现文件中添加搜索栏对我有用。

The relevant methods are the following. 相关方法如下。

-(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;
}

I just had the same inquiry, and I came up with a half-baked but working solution that does not involve rewriting UISearchDisplayController. 我只是有同样的询问,我想出了一个半生不熟但工作的解决方案,不涉及重写UISearchDisplayController。

(END RESULT: A UITableView --that answers to shouldReloadTableForSearchString-- overlaid on top of the UICollectionView, once you click search it's dismissed, and you have your results in the collectionView. Read on if this is of interest) (结束结果:一个UITableView - 可以回答shouldReloadTableForSearchString--覆盖在UICollectionView之上,一旦你点击搜索它就被解散了,你将你的结果放在collectionView中。如果感兴趣请继续阅读)

In the IB created a UIViewController in which I inserted(see screenshot): a view for layout purposes --> in which I first dropped a UISearchBar and display controller. 在IB中创建了一个UIViewController,我插入了它(参见截图):一个用于布局目的的视图 - >其中我首先删除了一个UISearchBar并显示控制器。 In the same view (side by side) I dropped a UICollectionView with a custom UICollectionViewCell. 在同一个视图中(并排)我删除了一个带有自定义UICollectionViewCell的UICollectionView。 Then I dropped in a UITableViewProvider (with a custom UITableCell, but that's not required, you can also ignore the UITabBarItem and the Nav item in the screenshot, that's inconsequential ) 然后我放入了一个UITableViewProvider(使用自定义UITableCell,但这不是必需的,你也可以忽略屏幕截图中的UITabBarItem和Nav项,这是无关紧要的)

I set the height of the UITableView to 0, and wired all the Outlets and delegates, and the net result is the following, when the cursor enters the UISearchBox, the UITableView overlay on top the UICollectionView, as one types the shouldReloadTableForSearchString del gets called and the results appear in the tableView; 我将UITableView的高度设置为0,并连接所有Outlets和委托,最终结果如下,当光标进入UISearchBox时,UITableView覆盖在UICollectionView的顶部,因为一个类型的shouldReloadTableForSearchString del被调用,结果出现在tableView中; On searchBarSearchButtonClicked I simply set the dataSource of the UICollectionView and call reloadData on it's outlet, et Voila. 在searchBarSearchButtonClicked上,我只需设置UICollectionView的dataSource,并在它的插座上调用reloadData,et Voila。

One would think Apple should generecize the search display controller; 有人会认为Apple应该对搜索显示控制器进行基因识别; maybe in a future release? 也许在未来的版本中?

( This works because that's the way the UISearchDisplay controller is optimized, IIRC there's actually one UITableView per character entry stacked on top of each other ) (这是因为这是优化UISearchDisplay控制器的方式,IIRC实际上每个字符条目的一个UITableView堆叠在彼此之上)

(Not posting code because there's quite a bit involved; plz inquire if anything is not straightforward) (不发布代码,因为涉及到很多; plz询问是否有任何不直接)

在此输入图像描述

Here just a swift code - worked for me! 这里只是一个快速的代码 - 为我工作!

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何决定使用 UICollectionViewController 还是 UIViewController? - How can we decide to use UICollectionViewController or UIViewController? 如何在 UICollectionViewController 的标头中访问 UISegmentedControl? - How can I access UISegmentedControl in header of UICollectionViewController? 如何在UICollectionviewcontroller中显示文本字段? - How can I show the textfield in the UICollectionviewcontroller? 我如何使用UISearchBar和UISearchDisplayController - How do I use the UISearchBar and UISearchDisplayController 如何保存UISearchDisplayController的状态? - How can I save the state of an UISearchDisplayController? UISearchDisplayController - 我如何禁用自动搜索 - UISearchDisplayController - how I can disable the automatic search 如何在uicollectionviewcontroller上使用触摸事件 - how to use touch events on uicollectionviewcontroller 如何在UICollectionViewController上方使用UIToolbar? - how to use UIToolbar above UICollectionViewController? 如何实现从UICollectionViewController到UIViewController的自定义UIViewController转换? - How can I achieve this custom UIViewController Transition from UICollectionViewController to UIViewController? 如何创建一个转接表单 UIViewController 到 UICollectionViewController? - 以编程方式 - How can I create a segue form UIViewController to UICollectionViewController ? - Programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM