搜索结果未显示正确的单词

searchResults not showing correct words

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

我用来创建此搜索结果的代码是,

    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
}

有什么想法吗?

使用此功能过滤内容:

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()
    }
}

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

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

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

此外,正如其他用户在评论中指出的那样,您真的应该检查一下您的 cellForRowAt。试试这个 link: