2014-07-11 26 views
0

我正在開發一個使用storyboard的iPad應用程序。我的storyboard中有兩個名稱爲nameCollectionViewdataColletionView的收藏視圖以及一個名爲languageTableView的表視圖。我需要爲nameCollectionView實施搜索欄。對於此CollectionView我使用來自resultArray的數據配置單元。如何爲Collection視圖設置Searchbar

我的第一個問題是:
1)是否可以爲收藏視圖設置搜索欄?
2)如何爲我的名字集視圖設置搜索欄?

有人幫我實現了以下功能。你

+1

主要的想法是,你沒有設置爲的CollectionView搜索欄;您爲resultArray中的數據設置searchBar,然後使用searchResult中的數據實例化newCollectionView,並將其添加到initialCollectionView –

回答

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]; 

}

+0

其頂部。 – user3762713

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{ 
     [email protected]""; 
    } 

    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]; 
    } } 
1

CollectionView + SearchBar with Swift3 + Storyboard的實現。

創建頁眉查看:

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

絕招:將搜索家庭AR代表對集合視圖類,搜索欄出口前進到CUSTOM REUSABLE視圖類

Connect the search bar delegate to collection view class

實現協議

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() 

    } 

的的CollectionView頭方法設置搜索欄代表

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() 
    } 
} 
相關問題