ios - 当结果不匹配时,搜索栏过滤器不会清除表格

标签 ios swift xcode filter searchbar

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

我还希望其中一个单元格在未找到匹配项时显示一条消息(例如“无可用结果”)。

这是我的代码:

@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 还是空字符串

关于ios - 当结果不匹配时,搜索栏过滤器不会清除表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46205188/

相关文章:

xcode - iPad Pro 尺寸分类

ios - 将数据传递到本地 html iOS webview

swift - 我的 segue 无法在 imagepicker 中工作

objective-c - 如何使用基类对象定义初始化器来填充基类属性?

ios - 文件应用程序 View Controller ?

ios - 下载网页内容后快速更改标签文本

string - 难以将文本文件分解为数组(Swift)

ios - Swift 闭包从函数返回

swift - 可以在Apple Watch模拟器中播放音频吗? - Xcode 10.1

ios - os.log 是做什么用的?