如果使用UISearchController重新加载数据,导航栏将消失

时间:2014-11-24 04:58:11

标签: ios iphone swift uisearchcontroller

我的应用程序工作,以便在UICollectionViewController内的可重用视图中有一个UISearchController。在搜索栏中点击时,我点击任何结果,它会触发通知重新加载collectionView以反映更改。

lazy var searchController: UISearchController = {

    let tableViewController = UITableViewController(style: .Plain)
    tableViewController.tableView.dataSource = self
    tableViewController.tableView.delegate = self
    tableViewController.tableView.registerClass(StoreListCell.self, forCellReuseIdentifier: "MiniStoreCell")

    let searchController = UISearchController(searchResultsController: tableViewController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(self.collectionView.frame), height: 44)
    return searchController
    }()

 override func viewDidLoad() {
    super.viewDidLoad()
    definesPresentationContext = true

    collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SearchView")
    let layout = collectionView.collectionViewLayout as UICollectionViewFlowLayout
    layout.headerReferenceSize = CGSize(width: CGRectGetWidth(collectionView.frame), height: 44)

    token = RLMRealm.defaultRealm().addNotificationBlock({ [unowned self] (notification, realm) -> Void in
      self.categories = Category.allObjects().sortedResultsUsingProperty("name", ascending: true)
      self.collectionView.reloadData()
    })
  }
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    let searchView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "SearchView", forIndexPath: indexPath) as UICollectionReusableView
    searchView.addSubview(searchController.searchBar)
    searchController.searchBar.sizeToFit()
    return searchView
  }

由于reloadData还重新加载搜索栏所在的可重用视图,这会导致导航栏消失,让我陷入searchresultsController enter image description here

enter image description here

0 个答案:

没有答案