【问题标题】:How can I use UISearchDisplayController with UICollectionViewController?如何将 UISearchDisplayController 与 UICollectionViewController 一起使用?
【发布时间】:2012-09-19 13:34:24
【问题描述】:

如果您将 UISearchDisplayController 与 UITableViewController 一起使用,则当用户点击搜索栏时,它会动画化以替换导航栏。

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

【问题讨论】:

  • 嘿,你在这个问题上有什么进展吗?我正在尝试做同样的事情......到目前为止我所做的只是添加一个搜索栏作为部分标题,但它并没有真正呈现正常。
  • 有人吗?我也可以使用它...似乎目前最好的解决方案可能是子类化 UISearchBar/UISearchDisplayController 并教他们如何与 UICollectionView 交互。
  • 我一直在努力尝试复制 uisearchdisplaycontroller,但这并不容易。

标签: ios uitableview uisearchbar uisearchdisplaycontroller uicollectionview


【解决方案1】:

我必须以编程方式将 searchBar 添加为 UICollectionReusableView 的子视图,我无法通过 IB 让它工作,因为我在分配插座时不断收到原型错误。在实现文件中添加搜索栏对我有用。

相关方法如下。

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

【讨论】:

  • 你的SupplementaryView 是什么?
【解决方案2】:

我刚刚进行了同样的询问,但我想出了一个不成熟但可行的解决方案,它不涉及重写 UISearchDisplayController。

(END RESULT:一个 UITableView——对 shouldReloadTableForSearchString 的回答——覆盖在 UICollectionView 的顶部,一旦你点击搜索,它就会被关闭,并且你的结果会出现在 collectionView 中。如果感兴趣,请继续阅读)

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

我将 UITableView 的高度设置为 0,并将所有的 Outlets 和 delegates 连接起来,最终结果如下,当光标进入 UISearchBox 时,UITableView 覆盖在 UICollectionView 顶部,作为一种类型 shouldReloadTableForSearchString del被调用,结果出现在 tableView 中;在 searchBarSearchButtonClicked 上,我只需设置 UICollectionView 的 dataSource 并在其插座上调用 reloadData 等等。

有人会认为 Apple 应该通用化搜索显示控制器;也许在未来的版本中?

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

(不贴代码,因为涉及很多;请询问是否有任何不直截了当的地方)

【讨论】:

  • 你实现了这个还是只是理论?请详细说明覆盖哪些委托方法。谢谢。
【解决方案3】:

这里只是一个快速代码 - 对我有用!

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2021-01-02
    • 1970-01-01
    • 2023-03-13
    • 2014-09-25
    相关资源
    最近更新 更多