Instagram Explorer SearchBar和TableView

时间:2015-08-02 20:42:01

标签: swift uitableview uisearchbar instagram

我有几天关于UISearchBar的问题。我不明白Instagram Explorer页面的搜索栏是如何工作的,例如:http://imgur.com/40I38mn

我的意思是,在开始时,ViewController显示了searchBar,并且在视图(tableview?)下面与它无关。当用户通过触摸搜索栏开始搜索时,会出现带有数据的右表视图。

实际上,有多个TableView吗? UISearchBar调用另一个视图?并且...简单的UISearchBar或UISearchController?我做了很多研究,测试了很多东西,我甚至尝试了一个tableView.hidden = true和if / else条件所以...你可以看到我有多失望Ahah。

有人可以向我解释一下显示器的结构吗?

3 个答案:

答案 0 :(得分:5)

它看起来是UISearchController,其搜索栏位于UICollectionView的标头内。当你说“哪个与它无关”时,UICollectionView就是你所指的观点。

访问搜索UITextField后,将显示搜索功能或右表视图。此视图似乎包含UISegmentedControl

UISegmentedControl教程应该可以帮助您设置类似效果的选项卡。 Here's我过去常用的一个非常好的。此外,您可以使用管理2个部分的UICollectionView构建一个非常相似的用户界面。第一部分包含标签&&第二个包含搜索结果列表。

我已经为你做了一些搜索,但我没有找到任何教程或一步一步的细分你如何实现它。如果您对表视图非常熟悉,我建议您尝试使用UICollectionView方法。如果没有,分段控制教程链接应该可以帮助您入门。

祝你好运,我希望这会有所帮助

更正(编辑

this问题似乎有助于描述如何将UISegmentedControl添加到UISearchController

答案 1 :(得分:1)

我的解决方案由许多其他SO帖子和Apple官方文档组成。 它在UINavigationBar中创建了一个带有搜索栏的视图(所以你需要嵌入你的NavigationController,但你可以改变一些线来避免这种情况)。 然后,您可以选择此SearchBar,它将调暗ViewController, 然后在输入一些搜索后它将自动完成,并且当点击SearchButton时开始实际搜索。

class ViewController: UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
//Our SearchController
  var searchController: UISearchController!

  override func viewDidLoad() {
    super.viewDidLoad()
    let src = SearchResultTVC() //A simple UiTableViewController I instanciated in the storyboard

    // We instanciate our searchController with the searchResultTCV (we he will display the result
    searchController = UISearchController(searchResultsController: src)

    // Self is responsible for updating the contents of the search results controller
    searchController.searchResultsUpdater = self

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

    // Dim the current view when you sélect eh search bar (anyway will be hidden when the user type something)
    searchController.dimsBackgroundDuringPresentation = true

    // Prevent the searchbar to disapear during search
    self.searchController.hidesNavigationBarDuringPresentation = false

    // Include the search controller's search bar within the table's header view
    navigationItem.titleView = searchController.searchBar

    definesPresentationContext = true
}

然后另一个功能是在修改搜索栏中的文本时调用的那个:

   func updateSearchResultsForSearchController(searchController: UISearchController) {
    // If dismissed then no update
    if !searchController.active {
        return
    }

    // Write some code for autocomplete (or ignore next fonction and directly process your data hère and siplay it in the searchResultTCV)

}

我的选择是让updateSearchResultsForSearchControlller()执行自动完成,然后在用户按搜索时加载结果,所以我有最后的功​​能:

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

    if let search = searchController.searchBar.text {
            // Load your results in searchResultTVC
        }
}

答案 2 :(得分:0)

我认为首先是UISearchBar然后可能是一种UISegementedControl,当你点击它时会先改变搜索名称的帐号或标签(Tab“Top”),人们用他们的名字(Tab“People”)等......