如何在新视图中显示 UISearchController 结果而不显示 tableView 的背景?
How do I display UISearchController results in a new view and not show the background of the tableView?
小问题,我正在使用 UISearchController,它运行良好。
但我想知道 select 搜索栏时是否可以显示新视图?
因为我搜索的时候不想看到我的 tableView/background.
您指的是UISearchController.
的表示上下文
Here is a link Apple 关于 definesPresentationContext
的文档以及我们关心的相关信息
this property controls which existing view controller in your view
controller hierarchy is actually covered by the new content
如果您仍在使用之前的 ,那么您已经快完成了,只需要查看 viewDidLoad()
中的以下代码行:
self.definesPresentationContext = true
默认值为 false
。由于它设置为 true,我们告诉 UITableViewController
当视图控制器或其后代之一呈现视图控制器时它将被覆盖。在我们的例子中,我们用 UISearchController 覆盖了 UITableViewController。
要解决您的问题,隐藏 tableView/background 就像在搜索栏处于活动状态时清除或切换 table 的数据源一样简单。这是在以下代码中处理的。
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) 在具有所需 labels/images 的故事板中添加一个新的 UIView
作为 UITableViewController
的 TableView
的子项。这将在您可能拥有的任何原型单元旁边。
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
}
}
希望对您有所帮助!
小问题,我正在使用 UISearchController,它运行良好。
但我想知道 select 搜索栏时是否可以显示新视图?
因为我搜索的时候不想看到我的 tableView/background.
您指的是UISearchController.
的表示上下文Here is a link Apple 关于 definesPresentationContext
的文档以及我们关心的相关信息
this property controls which existing view controller in your view controller hierarchy is actually covered by the new content
如果您仍在使用之前的 viewDidLoad()
中的以下代码行:
self.definesPresentationContext = true
默认值为 false
。由于它设置为 true,我们告诉 UITableViewController
当视图控制器或其后代之一呈现视图控制器时它将被覆盖。在我们的例子中,我们用 UISearchController 覆盖了 UITableViewController。
要解决您的问题,隐藏 tableView/background 就像在搜索栏处于活动状态时清除或切换 table 的数据源一样简单。这是在以下代码中处理的。
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) 在具有所需 labels/images 的故事板中添加一个新的 UIView
作为 UITableViewController
的 TableView
的子项。这将在您可能拥有的任何原型单元旁边。
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
}
}
希望对您有所帮助!