【问题标题】:How set Searchbar for Collection view如何为集合视图设置搜索栏
【发布时间】:2014-07-11 12:22:51
【问题描述】:

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

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

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

【问题讨论】:

  • 主要思想是,你不要为collectionView设置searchBar;您为 resultArray 中的数据设置 searchBar,然后使用 searchResult 中的数据实例化一个 newCollectionView 并将其添加到 initialCollectionView 的顶部

标签: ios objective-c storyboard uicollectionview uisearchbar


【解决方案1】:

CollectionView + SearchBarSwift3 + Storyboard 实现。

创建标题视图:

创建搜索栏:

创建可重用视图自定义类

设置可复用视图自定义类

创建搜索栏出口

技巧:将搜索栏代理连接到 COLLECTION VIEW 类,搜索栏出口转到 CUSTOM REUSABLE 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()
    }
}

【讨论】:

    【解决方案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];
    

    }

    【讨论】:

      【解决方案3】:
      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];
          } }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多