gpt4 book ai didi

ios - 为什么搜索不起作用?

转载 作者:行者123 更新时间:2023-11-30 13:35:30 24 4
gpt4 key购买 nike

我有一个数组,我正在尝试进行实时搜索。因此,最初我将拥有未过滤的数组,并且当我搜索时, TableView 将在我键入时更新。我已经设置了委托(delegate),所以我不知道为什么它不起作用。如有任何帮助,我们将不胜感激:)

@IBOutlet weak var table: UITableView!
var searchController = UISearchController()
var unfilitered = ["cat", "dog", "bat", "tiger"]
var filtered = [String]()
var shouldShowSearchResults = false

override func viewDidLoad() {
super.viewDidLoad()

configureSearchController()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: searchcell = table.dequeueReusableCellWithIdentifier("CELL") as! searchcell

if shouldShowSearchResults {
cell.animal.text = filtered[indexPath.row]
}
else {
cell.animal.text = unfiltered[indexPath.row]
}

return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowSearchResults {
return filtered.count
}
else {
return unfiltered.count
}
}

func configureSearchController() {
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = "Search"
searchController.searchBar.sizeToFit()
table.tableHeaderView = searchController.searchBar
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
shouldShowSearchResults = true
table.reloadData()
}


func searchBarCancelButtonClicked(searchBar: UISearchBar) {
shouldShowSearchResults = false
table.reloadData()
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
if !shouldShowSearchResults {
shouldShowSearchResults = true
table.reloadData()
}

searchController.searchBar.resignFirstResponder()
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchString = searchController.searchBar.text


filtered = unfiltered.filter({ (animal) -> Bool in
let animalText: NSString = animal

return (animalText.rangeOfString(searchString!, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
})

table.reloadData()
}

最佳答案

除非在主线程上执行,否则 reloadData 方法不会强制重绘 tableView。

关于ios - 为什么搜索不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120697/

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