gpt4 book ai didi

ios - 将 UISearchController 与 UINavigationController 一起使用

转载 作者:可可西里 更新时间:2023-11-01 05:43:55 26 4
gpt4 key购买 nike

我遇到了与 here 相同的问题,没有任何答案。 (将 searchController.active 设置为 false 会清除我不想要的搜索文本。)我想使用 UISearchController 以便用户可以搜索列表我的 UITableView 中的项目。我在 Interface Builder 中连接了一个 show segue,它在选择 TableView 的单元格时触发。 问题是,如果用户搜索某些内容,然后单击表格的某个单元格,当它转到新 View 时,搜索栏就在那里。理想情况下,我希望在用户搜索时导航栏被搜索栏“替换”,然后当用户单击单元格时导航栏返回,然后当用户单击时搜索栏返回“返回键。 (这是现在已弃用的 UISearchDisplayController 的工作方式。)我怎样才能做到这一点?这是我的 TableView 的 Controller 。

class ItemSearchViewController: UITableViewController, UISearchResultsUpdating
{
var searchController: UISearchController?

let itemList = [ItemList(category:"Chocolate", name:"chocolate Bar", price: 1234),
ItemList(category:"Chocolate", name:"chocolate Chip", price: 1234),
ItemList(category:"Chocolate", name:"dark chocolate", price: 1234),
ItemList(category:"Hard", name:"lollipop", price: 1234),
ItemList(category:"Hard", name:"candy cane", price: 1234),
ItemList(category:"Hard", name:"jaw breaker", price: 1234),
ItemList(category:"Other", name:"caramel", price: 1234),
ItemList(category:"Other", name:"sour chew", price: 1234),
ItemList(category:"Other", name:"gummi bear", price: 1234)]

var filteredList : [ItemList] = []

override func viewDidLoad()
{
super.viewDidLoad()

self.title = "Item Search"
self.tableView.delegate = self
self.tableView.dataSource = self

self.searchController = UISearchController(searchResultsController: nil)
self.searchController!.searchResultsUpdater = self
self.searchController!.hidesNavigationBarDuringPresentation = true
self.searchController!.dimsBackgroundDuringPresentation = false
self.searchController!.searchBar.searchBarStyle = .Minimal
self.searchController!.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.searchController!.searchBar
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "itemDetail")
{
let itemDetailViewController = segue.destinationViewController as UIViewController
let selectedCell = sender as UITableViewCell
itemDetailViewController.title = selectedCell.textLabel?.text
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
var item : ItemList

if self.searchController!.active
{
item = self.filteredList[indexPath.row]
}
else
{
item = self.itemList[indexPath.row]
}

cell.textLabel!.text = item.name
return cell
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if (self.searchController!.active)
{
return self.filteredList.count
}
else
{
return self.itemList.count
}
}
}

extension ItemSearchViewController: UISearchResultsUpdating
{
func updateSearchResultsForSearchController(searchController: UISearchController)
{
if (searchController.searchBar.text.isEmpty)
{
self.filteredList = self.itemList
}
else
{
let searchPredicate =
{
(item: ItemList) -> Bool in
item.name.rangeOfString(searchController.searchBar.text, options: .CaseInsensitiveSearch) != nil
}
self.filteredList = self.itemList.filter(searchPredicate)
}
self.tableView.reloadData()
}
}

最佳答案

viewDidLoad()中添加这一行

definesPresentationContext = true

来自 definesPresentationContext 的文档

A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.

讨论

When a view controller is presented, iOS starts with the presenting view controller and asks it if it wants to provide the presentation context. If the presenting view controller does not provide a context, then iOS asks the presenting view controller's parent view controller. iOS searches up through the view controller hierarchy until a view controller provides a presentation context. If no view controller offers to provide a context, the window's root view controller provides the presentation context.

If a view controller returns true, then it provides a presentation context. The portion of the window covered by the view controller's view determines the size of the presented view controller's view. The default value for this property is false.

关于ios - 将 UISearchController 与 UINavigationController 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29618959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com