gpt4 book ai didi

ios - 如何从 searchResultsController (UISearchController) 更新数据

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

所以我使用 searchResultsController,它接受一个字符串数组,并在表格 View 中显示它们(它是一个自动完成列表)。当用户按下键盘上的“搜索”按钮时,输入的字符串尚未在我的表格 View 中,我想添加它,并相应地更新表格 View 。

问题是,一旦我将字符串添加到数组并进行新搜索,数组就不会使用新值进行更新!

这是我的代码:

在 Overview.swift 类的 ViewDidLoad() 中

    class Overview: UIViewController,UISearchControllerDelegate,UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource {

var mySearchController : UISearchController!
var mySearchBar : UISearchBar!

override func viewDidLoad() {
super.viewDidLoad()
let src = SearchResultsController(data: convertObjectsToArray())
// instantiate a search controller and keep it alive
mySearchController = UISearchController(searchResultsController: src)
mySearchController.searchResultsUpdater = src
mySearchBar = mySearchController.searchBar
//set delegates
mySearchBar.delegate = self
mySearchController.delegate = self
}

这是数据函数,用于UISearchController

   func convertObjectsToArray() -> [String] {

//open realm and map al the objects
let realm = try! Realm()
let getAutoCompleteItems = realm.objects(AutoComplete).map({$0})

...
return convertArrayStrings // returns [String] with all words
}

因此,当用户按下键盘上的搜索按钮时,我将该单词保存到我的数据库中。

现在我需要将 convertObjectsToArray() 的更新版本放入我的 searchResultsController 中,但我还没有找到如何执行此操作。欢迎所有帮助

最后但并非最不重要的是,我的 SearchResultsController 类,它在我的 Overview.swift 类的 viewDidLoad 中使用。

    class SearchResultsController : UITableViewController {
var originalData : [String]
var filteredData = [String]()

init(data:[String]) {
self.originalData = data
super.init(nibName: nil, bundle: nil)
}

required init(coder: NSCoder) {
fatalError("NSCoding not supported")
}

override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredData.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel!.text = self.filteredData[indexPath.row]
return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
clickedInfo = filteredData[indexPath.row]
}



override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

}

为了在表格 View 中过滤我的单词(当用户键入内容时,仅显示匹配的字符串),我使用以下扩展。

extension SearchResultsController : UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
let sb = searchController.searchBar
let target = sb.text!
self.filteredData = self.originalData.filter {
s in
let options = NSStringCompareOptions.CaseInsensitiveSearch
let found = s.rangeOfString(target, options: options)
return (found != nil)
}
self.tableView.reloadData()
}

最佳答案

我认为您可以使用搜索 Controller 的更新功能:

func updateSearchResultsForSearchController(searchController: UISearchController) {

convertObjectsToArray()
self.tableView.reloadData()


}

关于ios - 如何从 searchResultsController (UISearchController) 更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789709/

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