如何在新视图中显示UISearchController结果而不显示tableView的背景?

时间:2015-12-05 22:17:04

标签: ios swift uisearchcontroller

快速提问,我正在使用UISearchController,它的工作非常完美。

但我想知道在选择搜索栏时是否可以显示新视图?

因为我在搜索时不想看到我的tableView / background。

1 个答案:

答案 0 :(得分:3)

您所指的是 UISearchController的演示文稿上下文

Here is a link访问Apple关于definesPresentationContext的文档以及我们关注的相关信息

  

此属性控制视图中的现有视图控制器   控制器层次结构实际上由新内容覆盖

如果您之前仍在使用this example UISearchController,那么您已经快完成了,只需要查看viewDidLoad()内的以下代码行:

self.definesPresentationContext = true

此默认值为false。由于它设置为true,我们告诉UITableViewController当视图控制器或其后代之一呈现视图控制器时它将被覆盖。在我们的例子中,我们用UISearchController覆盖了UITableViewController。

要解决您的问题,隐藏tableView / background就像在搜索栏处于活动状态时清除或切换表格的数据源一样简单。这在以下代码中处理。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (self.userSearchController.active) {
        return self.searchUsers.count
    } else {
        // return normal data source count
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("userCell") as! UserCell
    if (self.userSearchController.active && self.searchUsers.count > indexPath.row) {
        // bind data to the search data source
    } else {
        // bind data to the normal data source
    }
    return cell
}

当搜索栏被解除时,我们想要重新加载正常数据源,完成以下操作:

func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    // Clear any search criteria
    searchBar.text = ""

    // Force reload of table data from normal data source
}

这里有一个关于UISearchControllers的link to a great article,并简要概述了它们的内部工作方式和视图层次结构。

对于SO上的未来帖子,您应该始终尝试包含相关的代码示例,以便人们能够尽可能提供最佳反馈:)

修改

我想我有点误解了你的问题,但上述内容仍然与答案相关。要在搜索结果为空或未输入任何内容时显示特殊视图,请执行以下操作:

1)在故事板中添加一个新的UIView作为TableView UITableViewController的孩子,并添加所需的标签/图片。这将是您可能拥有的任何原型单元旁边。

2)在UITableViewController

中创建并连接插座
@IBOutlet var emptyView: UIView!
@IBOutlet weak var emptyViewLabel: UILabel!

3)最初在viewDidLoad()

中隐藏视图
self.emptyView?.hidden = true

4)创建一个帮助函数来更新视图

func updateEmptyView() {
    if (self.userSearchController.active) {
        self.emptyViewLabel.text = "Empty search data source text"
        self.emptyView?.hidden = (self.searchUsers.count > 0)
    } else {
        // Keep the emptyView hidden or update it to use along with the normal data source
        //self.emptyViewLabel.text = "Empty normal data source text"
        //self.emptyView?.hidden = (self.normalDataSource.count > 0)
    }
}

5)在您完成查询后调用updateEmptyView()

func loadSearchUsers(searchString: String) {
    var query = PFUser.query()

    // Filter by search string
    query.whereKey("username", containsString: searchString)

    self.searchActive = true
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        if (error == nil) {
            self.searchUsers.removeAll(keepCapacity: false)
            self.searchUsers += objects as! [PFUser]
            self.tableView.reloadData()
            self.updateEmptyView()
        } else {
            // Log details of the failure
            println("search query error: \(error) \(error!.userInfo!)")
        }
        self.searchActive = false
    }
}

希望有所帮助!