swift - 提高搜索速度结果 - swift tableviewcontroller

标签 swift xcode uitableview search

我正在构建个人天气应用程序,用户可以在其中选择城市并显示天气温度。

我在此 JSON 文件中使用搜索:

cities.json

我使用以下代码进行搜索:

搜索扩展:

extension SearchUI: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
    filterContentForSearchText(searchText: searchController.searchBar.text!)
}

过滤功能:

func filterContentForSearchText(searchText: String) {
    filteredCountries = countries.array!.filter { country in
        return country["name"].stringValue.lowercased().hasPrefix(searchText.lowercased())
    }
    tableView.reloadData()
}

我的 cellForRowAt 函数:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell : UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "countryCell")


    var data: JSON

    if searchController.isActive && searchController.searchBar.text != "" {
        data = filteredCountries[indexPath.row]
    } else {
        data = countries[indexPath.row]
    }

    let countryName = data["country"].stringValue
    let countrycity = data["name"].stringValue
    //data1.append(data["name"].stringValue)
    cell?.textLabel?.text = countrycity
    cell?.detailTextLabel?.text = countryName

    return cell!
}

我的应用程序中的搜索过程非常慢,并且在过滤过程中键盘输入速度很慢。

通知: 国家 - 是一个 JSON 对象

谢谢大家!

最佳答案

解析:

countries = try! JSON(data: NSData(contentsOfFile: Bundle.main.path(forResource: "cities_with_countries", ofType: "json")!)! as Data)

关于swift - 提高搜索速度结果 - swift tableviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44433194/

相关文章:

ios - 核心数据删除一个元素 NSManagedObject Swift

iphone - 如何将 uitableview 选择与 indexpath 值居中

json - 解析一些网站数据(SWIFT/JSON)

swift - 在 Xcode 7.3.1 中,为什么所有警告在我的 .swift 文件中都不可见

swift - UITableViewController 和自动更新结果

swift - 在 Swift 中检查是否为 "dictionary item exists and is not blank"的紧凑方法?

ios - 我应该将私有(private) IBOutlet 设为内部以便进行单元测试吗?

ios - dispatch 组通知安置?

iphone - 更改电话号码时如何使光标不跳到字段末尾?

ios - 无法跟踪内存问题