16

如果您將UISearchDisplayController與UITableViewController一起使用,那麼當用戶點擊搜索欄時,它會動畫以替換導航欄。如何在UICollectionViewController中使用UISearchDisplayController?

我想在UICollectionViewController的頂部使用一個UISearchBar時拿到同樣的效果。有任何想法嗎?

+0

嘿,你在這個問題上有什麼進展嗎?我正在嘗試做同樣的...直到現在我所做的所有事情都是添加一個搜索欄作爲節標題,但它並沒有真正呈現正常。 – aspyct

+0

我已經沒有什麼:(。 –

+0

任何??我可以利用這個以及...好像在這一點上最好的解決方案可能是隻子類的UISearchBar/UISearchDisplayController,教他們如何與UICollectionView互動。 – Raconteur

回答

2

我不得不以編程方式添加搜索欄作爲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; 
} 
+0

什麼是你的'SupplementaryView'? – AaoIi

1

我有同樣的疑問,我想出了一個半生不熟的,但工作的解決方案,不涉及改寫UISearchDisplayController。

(最終結果是:凹口 - 答案shouldReloadTableForSearchString--一個UITableView覆蓋在UICollectionView的頂部,一旦你點擊搜索它的開除,你必須在你的CollectionView結果閱讀,如果這是令人感興趣的。)

在IB中創建一個UIViewController我在其中插入(見截圖): 佈局的目的的視圖 - >在其中予先下降一個UISearchBar和顯示控制器。 在相同的視圖(並排)中,我放下了一個帶有自定義UICollectionViewCell的UICollectionView。然後我放入一個UITableViewProvider(帶有一個自定義的UITableCell,但這不是必需的,你也可以忽略屏幕截圖中的UITabBarItem和Nav項,這是無足輕重的)

我將UITableView的高度設置爲0,所有的Outlets和委託,最終的結果如下,當光標進入UISearchBox,UITolViewView上方的UITableView疊加層時,作爲一種類型,shouldReloadTableForSearchString del被調用並且結果出現在tableView中;在searchBarSearchButtonClicked上,我只需設置UICollectionView的dataSource並在它的出口et voila上調用reloadData。人們會認爲蘋果應該重新整合搜索顯示控制器;也許在未來的版本?

(這工作,因爲這是UISearchDisplay控制器優化的方式,IIRC居然還有每一個堆疊在另一個上面的字符輸入一個的UITableView)

(不張貼代碼,因爲有相當複雜; PLZ詢問是否事情並不簡單)

enter image description here

+1

你是否實現了這個或者只是理論?請詳細說明重寫哪些代表方法。謝謝。 – chandu

0

這裏只是一個SWIFT代碼 - 爲我工作!

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