Ios 如何设置集合视图的搜索栏

Ios 如何设置集合视图的搜索栏,ios,objective-c,storyboard,uicollectionview,uisearchbar,Ios,Objective C,Storyboard,Uicollectionview,Uisearchbar,我正在使用storyboard开发一个iPad应用程序。在我的storyboard中,我有两个名为nameCollectionView和DataCollectionView的集合视图,还有一个名为languageTableView的表格视图。我需要为nameCollectionView实现搜索栏。对于此CollectionView我使用resultArray中的数据配置单元格 我的第一个问题是: 1) 是否可以为“收藏”视图设置搜索栏?2)如何为“我的姓名”收藏视图设置搜索栏 有人帮助我实现以下

我正在使用storyboard开发一个iPad应用程序。在我的storyboard中,我有两个名为
nameCollectionView
DataCollectionView
的集合视图,还有一个名为
languageTableView
的表格视图。我需要为
nameCollectionView
实现搜索栏。对于此
CollectionView
我使用
resultArray
中的数据配置单元格

我的第一个问题是:
1) 是否可以为“收藏”视图设置搜索栏?
2)如何为“我的姓名”收藏视图设置搜索栏


有人帮助我实现以下功能。

您也应该这样尝试:

- (void)searchBar:(UISearchBar *) searchBar textDidChange:(NSString *)searchText {
if (searchBar.text && [searchBar.text length]) {
    NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchBar.text];
    self.filteredData= [self.allData filteredArrayUsingPredicate:filterPredicate];

} else {
    self.filteredData = allData;
}

[nameCollectionView reloadData];
}

  • 是的,这是可能的。2.这是:
  • ==================================

    #pragma mark - Search Bar Delegate
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
        isSearching = true; }
    
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        //Remove first
        [self.filteredNames removeAllObjects];
        [self.filteredSections removeAllObjects];
    
        if([searchText length] != 0) {
            isSearching = true;
            // Filter the array using NSPredicate
    
            for (int i = 0; i<[self.allNames count]; i++) {
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
                NSArray *predicatedArray = [NSMutableArray arrayWithArray:[self.allNames[i] filteredArrayUsingPredicate:predicate]];
    
                if([predicatedArray count] != 0){
                    [self.filteredNames addObject:predicatedArray];
                    [self.filteredSections addObject:[self.orderedSections objectAtIndex:i]];
                }
            }
        }
        else {
            isSearching = false;
            self.filteredNames = [NSMutableArray arrayWithArray:self.allNames];
            self.filteredSections = [NSMutableArray arrayWithArray:self.orderedSections];
        }
        [self.collectionView reloadData]; }
    
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        [searchBar resignFirstResponder]; }
    
    
    
    
    
    
    #pragma mark - Collection Delegate Methods
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
        return [self.filteredNames count]; }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return [[self.filteredNames objectAtIndex:section] count]; }
    
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
        UICollectionReusableView *reusableview = nil;
    
    
        //Header
        //if (kind == UICollectionElementKindSectionHeader)
        NamesListCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"NamesListCollectionHeaderView" forIndexPath:indexPath];
        if (headerView==nil) {
            headerView  = [[[NSBundle mainBundle] loadNibNamed:@"NamesListCollectionHeaderView" owner:self options:nil] objectAtIndex:0];
        }
        if (self.filteredSections.count != 0) {
            //Text
            headerView.title.text=[self.filteredSections objectAtIndex:indexPath.section];
        }else{
            headerView.title.text=@"";
        }
    
        reusableview = headerView;
        return reusableview;
    
        //Footer
        //if (kind == UICollectionElementKindSectionFooter) {}
    
        return nil;
         }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        //Init of the Cell
        NameCollectionCellController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NameCollectionCell" forIndexPath:indexPath];
        if(cell == nil){
            cell = [[NameCollectionCellController alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 140.0f, 160.0f)];
        }
    
        //Name
        Name *curName;
    
        curName = [[self.filteredNames objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
        //Set Cell
        [cell setCell];
    
        //Set Selected
        if (curName == ((NamesViewController*)self.parentViewController).curName) {
            [cell setSelected:YES];
            [cell setSelectedBorder];
        }else{
            [cell setDeselected];
            [cell setSelected:NO];
        }
    
        //Title
        cell.nameValueLabel.text = curName.name;
    
        return cell; }
    
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
        [self collectionView:self.collectionView didDeselectItemAtIndexPath:self.curIndexPath];
    
        //Set Cur Name
        if ([self. filteredNames count]>0) {
    
            self.curIndexPath = indexPath;
            ((NamesViewController*)self.parentViewController).curName
    = [[self.filteredNAmes objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
            self.curCell = (NameCollectionCellController *)[collectionView cellForItemAtIndexPath:indexPath];
            [self.curCell setSelectedBorder];
            [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
    
            [self.collectionView reloadData];
    
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didSelectNameNotification" object:self];
        } }
    
    -(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath!=nil) {
            [self.curCell setDeselected];
        } }
    
    #pragma标记-搜索栏委托
    -(无效)searchBarTextDidBeginEditing:(UISearchBar*)搜索栏{
    isSearching=true;}
    -(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本{
    //先走
    [self.filteredNames removeAllObjects];
    [self.filteredSections removeAllObjects];
    如果([searchText length]!=0){
    isSearching=true;
    //使用NSPredicate筛选阵列
    对于(int i=0;i0){
    self.curindepath=indepath;
    ((NamesViewController*)self.parentViewController).curName
    =[[self.filteredNAmes objectAtIndex:indexath.section]objectAtIndex:indexath.row];
    self.curCell=(NameCollectionCellController*)[collectionView cellForItemAtIndexPath:indexPath];
    [self.curCell setSelectedBorder];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenter垂直动画:是];
    [self.collectionView-reloadData];
    [[NSNotificationCenter defaultCenter]postNotificationName:@“didSelectNameNotification”对象:self];
    } }
    -(void)collectionView:(UICollectionView*)collectionView未取消ItemAtIndexPath:(NSIndexPath*)indexPath{
    if(indexPath!=nil){
    [self.curCell setDeselected];
    } }
    
    集合视图+搜索栏带有Swift3+情节提要实现

    创建标题视图:

    创建搜索栏:

    创建可重用视图自定义类

    //MARK: - SEARCH
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        if(!(searchBar.text?.isEmpty)!){
            //reload your data source if necessary
            self.collectionView?.reloadData()
        }
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if(searchText.isEmpty){
            //reload your data source if necessary
            self.collectionView?.reloadData()
        }
    }
    

    设置可重用视图自定义类

    //MARK: - SEARCH
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        if(!(searchBar.text?.isEmpty)!){
            //reload your data source if necessary
            self.collectionView?.reloadData()
        }
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if(searchText.isEmpty){
            //reload your data source if necessary
            self.collectionView?.reloadData()
        }
    }
    

    创建搜索栏出口

    技巧:将搜索栏委托连接到集合视图类,搜索栏出口转到自定义可重用视图类

    实现协议的CollectionView标头方法

        override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    
             if (kind == UICollectionElementKindSectionHeader) {
                 let headerView:UICollectionReusableView =  collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CollectionViewHeader", for: indexPath)
    
                 return headerView
             }
    
             return UICollectionReusableView()
    
        }
    
    设置搜索栏代理

        class MyCollectionViewController: (other delegates...), UISearchBarDelegate {
    
    最后,将在CollectionView类中调用搜索栏委托方法

    //MARK: - SEARCH
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        if(!(searchBar.text?.isEmpty)!){
            //reload your data source if necessary
            self.collectionView?.reloadData()
        }
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if(searchText.isEmpty){
            //reload your data source if necessary
            self.collectionView?.reloadData()
        }
    }
    

    其主要思想是,不为collectionView设置搜索栏;而是为resultArray中的数据设置搜索栏,然后使用searchResult中的数据实例化一个新的collectionView,并将其添加到initialCollectionView的顶部