2016-11-02 5 views
1

Как я должен правильно это сделать? Я не знаю, как правильно интегрировать панель поиска с UICollectionView. Пожалуйста, помогите мне понять эту проблему.Панель поиска не отображается в заголовке UICollectionView при запуске моего проекта

P.S. Мой язык Swift

My view in Interface Builder [1]

My view when I run project [2]

 
import UIKit

class MainViewController: UICollectionViewController {

override func viewDidLoad() { 
    super.viewDidLoad() 
} 



override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 14 
} 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrendingGifCell", for: indexPath) 
    return cell 
} 

}

+0

Можете ли вы показать код? :) –

ответ

3

Try ниже код, он должен решить проблему:

class MainViewController: UICollectionViewController,UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating { 

    var searchController : UISearchController! 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     //Search at the top 
     self.searchController = UISearchController(searchResultsController: nil) 

     self.searchController.searchResultsUpdater = self 
     self.searchController.delegate = self 
     self.searchController.searchBar.delegate = self 

     self.searchController.hidesNavigationBarDuringPresentation = false 
     self.searchController.dimsBackgroundDuringPresentation = true 
     self.searchController.obscuresBackgroundDuringPresentation = false 


     searchController.searchBar.becomeFirstResponder() 

     self.navigationItem.titleView = searchController.searchBar 



} 



    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 14 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrendingGifCell", for: indexPath) 
     return cell 
    } 

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
    self.dismiss(animated: true, completion: nil) 
} 

func updateSearchResults(for searchController: UISearchController) 
{ 
    print("updating") 
} 


    } 

Дайте мне знать, как это работает :).

+0

У меня есть ошибки «Значение типа« MainViewController »не имеет статуса пользователя« SearchController »и« Использование неразрешенного идентификатора »searchController» –

+0

Извините, я забыл одну строку ...: D –

+0

Это работает, спасибо! :) –