ios - 我们可以将搜索应用于从 json 解析中获取的数组的 TableView 吗

标签 ios arrays swift uitableview

我正在 tableView 中搜索,但是 NSDictionry 没有成员项,它没有过滤搜索

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {arrFilterLocation = searchText.isEmpty ? arrInrestLocation:arrInrestLocation.filter({ (item:String) -> Bool in
        return item.range(of: searchText,options: .caseInsensitive ,range: nil,locale: nil) != nil
    })
   TabelView.reloadData()
}

最佳答案

假设arrFilterLocationarrInrestLocation都是String数组,即

var arrInrestLocation: [String]
var arrFilterLocation: [String]

您可以简单地在每个 string 上使用 contains(_:)filter 出所有 strings arrInrestLocation 包含 searchText

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    arrFilterLocation = searchText.isEmpty ? arrInrestLocation : arrInrestLocation.filter({ $0.lowercased().contains(searchText.lowercased()) })
    tableView.reloadData()
}

关于ios - 我们可以将搜索应用于从 json 解析中获取的数组的 TableView 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579675/

相关文章:

iphone - ARC 和 Storyboard 的兼容性

iphone - 突出显示单个烛条的核心图

ios - 核心数据 : What's the difference between performBackgroundTask and newBackgroundContext()?

ios - 归档应用程序时找不到 Swift Objective-C header

ios - 在 IOS Objective-C 代码中添加多个目标 header

ios - 将 String 转换为 Double,然后再转换回 String

ios - iOS 13 是否可以选择退出深色模式?

java - PdfPTables[] 数组填充时出现空指针异常

javascript - JSON 到数组不工作

php - 像数据库查询一样过滤掉数组