-2

我的 iOS 应用程序中有一个 searchResultsViewController,它显示一组数据供用户搜索。当我尝试搜索一个随机字母时,比如说 P,它不会显示任何包含 P 的单词。

在此处输入图像描述 在此处输入图像描述

我用来创建这个 searchResults 的代码是,

    var array = ["Assembly", "Auto Care", "Electronic Help", "Item Delivery", "Handyman", "House Chores", "Junk Removal", "Lawn & Yard Care", "Moving", "Painting", "Pet Care", "Seasonal Work"]
var selectedItems = [String]()
var searchController = UISearchController()
var filteredArray = [String]()
var resultsController = UITableViewController()


override func viewDidLoad() {
    super.viewDidLoad()
    
    searchController = UISearchController(searchResultsController: resultsController)
    tableView.tableHeaderView = searchController.searchBar
    searchController.searchResultsUpdater = self
    resultsController.tableView.delegate = self
    resultsController.tableView.dataSource = self
    searchController.searchBar.showsCancelButton = true
    searchController.searchBar.showsScopeBar = true
    searchController.searchBar.delegate = self
    
  let attributes = [NSAttributedString.Key.foregroundColor: GREEN_Theme]
  UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
  UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Done"


}


func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    done()
}

func updateSearchResults(for searchController: UISearchController) {
    filteredArray = array.filter({ (array:String) -> Bool in
        if array.contains(searchController.searchBar.text!) {
             return true
        } else {
        return false
        }
    })
    resultsController.tableView.reloadData()
    searchController.automaticallyShowsCancelButton = true
 }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if selectedItems.contains(array[indexPath.row]) {
selectedItems.remove(at: selectedItems.firstIndex(of: array[indexPath.row])!)
tableView.cellForRow(at: indexPath)?.accessoryType = .none
} else {
selectedItems.append(array[indexPath.row])
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
tableView.reloadData()
print(selectedItems)
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == resultsController.tableView {
        return filteredArray.count
        
    } else {
        return array.count
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = array[indexPath.row]
if selectedItems.contains(array[indexPath.row]) {
cell.accessoryType = .checkmark
}else{
cell.accessoryType = .none
}
return cell
}

有什么想法吗?

4

1 回答 1

2

使用此功能过滤内容:

extension SearchVC: UISearchBarDelegate{
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        fetchedData = []
        if searchText == ""{
            fetchedData = items
        } else {
            for words in items{
                if
                    words.item.lowercased().contains(searchText.lowercased()){
                    filteredData.append(words)
                }
            }
        }
        table.reloadData()
    }
}

WherefetchedData是一个空字符串数组,items是你的数组。
如果搜索栏为空fetchedData,则将填充您的所有项目,否则仅填充匹配的项目。

现在,最重要的是使用fetchedData而不是items正确显示结果和计数。因此,例如:

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

此外,正如其他用户在评论中指出的那样,您应该真正检查您的cellForRowAt. 试试这个链接:https ://stackoverflow.com/a/34345245/10408872

于 2020-12-29T06:31:55.567 回答