创建单独的UITableView以显示搜索结果

时间:2016-04-26 09:50:04

标签: ios cocoa-touch uisearchbar uisearchdisplaycontroller uisearchcontroller

当我在线搜索有关UISearchBar的教程时,他们通常会谈论在与显示未过滤数据的UITableVie相同的UITableVie中创建搜索栏。

我想创建一个UISearchBar和一个搜索按钮,然后使用第二个单独的tableview来显示搜索结果,就像谷歌的一些iOS应用程序一样。

我怎么能实现这个目标?

1 个答案:

答案 0 :(得分:0)

以下只是一个让你入门的例子。

首先定义视图控制器的位置:

    let contentRect = CGRectMake(0, 30, self.view.bounds.size.width, 100)

然后,您需要设置UITableViewController并设置委托和数据源,并将其作为子视图添加到主视图控制器:

    resultsController = UITableViewController(style: UITableViewStyle.Plain)
    resultsController.tableView.delegate = self
    resultsController.tableView.dataSource = self
    resultsController.view.frame = contentRect
    self.addChildViewController(resultsController)
    self.view.addSubview(resultsController.view)
    resultsController.didMoveToParentViewController(self)

您将整个代码放在要显示搜索结果的表格视图中。