如何设置Searchbar for Collection视图

时间:2014-07-11 12:22:52

标签: ios objective-c storyboard uicollectionview uisearchbar

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

我的第一个问题是:
1)是否可以为集合视图设置搜索栏?
2)如何为我的名称集合视图设置搜索栏?

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

3 个答案:

答案 0 :(得分:1)

使用 Swift3 + Storyboard 实施

CollectionView + SearchBar

创建标题视图

Creating the Header View

创建搜索栏:

Creating the Search Bar

创建可重复使用的视图自定义类

Create the reusable view custom class

设置可重复使用的视图自定义类

Set the reusable view custom class

创建搜索栏

Create the search bar outlet in custom class

技巧:将搜索栏代表连接到COLLECTION VIEW类,搜索栏出口转到CUSTOM REUSABLE VIEW CLASS

Connect the search bar delegate to collection view class

实现协议的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()
    }
}

答案 1 :(得分:0)

你也应该这样试试:

- (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 :(得分:0)

  1. 是的可能。这是:
  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];
        } }