当结果不匹配时,搜索栏过滤器不会清除 table

Search bar filter isn't clearing the table when results don't match

我有一个可用的搜索栏,现在我只需要帮助清除 table 当搜索文本与数组中的任何项目都不匹配时(不包括空搜索栏)。

我还希望其中一个单元格在未找到匹配项时显示一条消息(如 "no results available")。

这是我的代码:

@IBOutlet var searchForTool: UISearchBar!
@IBOutlet var toolTable: UITableView!

var searchActive : Bool = false
    {
    didSet {
        if searchActive != oldValue {
            toolTable?.reloadData()
        }
    }
}

typealias Item = (data: String, identity: String)

var filtered: [Item] = []
var items: [Item] = [
    (data: "  Data1", identity: "A"), (data: "  Data2", identity: "B")
]

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.setNavigationBarHidden(true, animated: false)

    AppState.shared.category = "Alphabetical"

}

@IBAction func backButton(_ sender: Any) {

    if let navController = self.navigationController {
        for controller in navController.viewControllers {
            if controller is ToolsViewController {
                navController.popToViewController(controller, animated: true)
            }
        }
    }
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = items.filter { item in
        item.data.localizedCaseInsensitiveContains(searchText)
    }

    searchActive = !filtered.isEmpty

    self.toolTable.reloadData()

}

func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(searchActive) {
        return filtered.count
    }

    return items.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell

    cell.alphabeticalLabel.layer.masksToBounds = true

    if(searchActive) {
        cell.alphabeticalLabel.text = filtered[indexPath.row].data
        cell.alphabeticalLabel.layer.cornerRadius = 10
    } else {
        cell.alphabeticalLabel.text = items[indexPath.row].data
        cell.alphabeticalLabel.layer.cornerRadius = 10
    }

    return cell
}

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

    let vcName: String
    if searchActive {
        vcName = filtered[indexPath.row].identity
    } else {
        vcName = items[indexPath.row].identity
    }
    let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
    self.navigationController?.pushViewController(viewController!, animated: true)
}
}

我知道这不是创建搜索栏功能的最佳方式,但这是我已经使用了一段时间的方式。我确定解决方案不是很复杂,但我只是运气不好。

如有任何帮助,我们将不胜感激。

根据您的回复,添加检查以查看搜索文本是否为空:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(searchActive) {
        return filtered.count
    }
    else if (searchForTool.text? == "")  { // check if this is "" or nil
        return items.count
   }
   else {
       return 0 // Or 1 if you want to show a cell with "no found" text
   }
}

您需要以类似方式调整 cellForRowAtIndexpath。并在搜索栏的文本 属性 为 nil 或用户未输入任何内容时检查空字符串