Swift 中的自动完成搜索列表问题

Autocomplete Search List issue In Swift

你好,我的搜索功能出现了一个非常奇怪的问题。我已经成功实现了搜索功能。我正在从后端服务获取数据。问题是在某些阶段,根据键入的关键字,数据不会在建议区域(tableView)中准确加载。我还在控制台上打印结果,以检查我是否根据关键字获得了准确的结果,并且控制台显示了准确的结果,只是建议区域有时不会加载准确的结果。例如在我的应用程序中如果我想搜索城市 "Lahore"。我输入了完整的字母 "Lahore"

显示这个

但是当我按 x 图标或退格键删除 "e" 它显示准确的结果

我只是举个例子。这几乎一直在发生。你能不能看看我的代码,看看我在做什么错。

class CountryTableViewController: UITableViewController, UISearchResultsUpdating {

    var dict = NSDictionary()
    var filteredKeys = [String]()

    var resultSearchController = UISearchController()

    var newTableData = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.resultSearchController = ({

            let controller  = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            self.tableView.tableHeaderView = controller.searchBar
            return controller


        })()

        self.tableView.reloadData()
    }

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

        if (self.resultSearchController.active) {

            return self.filteredKeys.count
        } else {

            return dict.count
        }

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell

        if(self.resultSearchController.active){





                let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)

               let stateName  = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["state_name"] as? NSString)

                 let shortName  = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["short_country_name"] as? NSString)


            if (cityName != "-" || shortName != "-"){
                cell.stateNameLabel.text = stateName as? String
                cell.cityNameLabel.text = cityName as? String
                 cell.shortNameLabel.text = shortName as? String

            }

                      return cell

        }else{
            if let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString){
            cell.cityNameLabel.text = cityName as String
            }
            return cell
        }



    }



    func updateSearchResultsForSearchController(searchController: UISearchController) {

        let searchWord = searchController.searchBar.text!

        getCountriesNamesFromServer(searchWord)

        self.filteredKeys.removeAll()

        for (key, value) in self.dict {

            let valueContainsCity: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["city_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false

            let valueContainsCountry: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["country_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false

            if valueContainsCity || valueContainsCountry{ self.filteredKeys.append(key as! String) }




        }

        self.tableView.reloadData()
    }




    func getCountriesNamesFromServer(searchWord:String){


        let url:String = "http://localhost"
        let params = ["keyword":searchWord]



        ServerRequest.postToServer(url, params: params) { result, error in

            if let result = result {
                print(result)



                self.dict = result

            }
        }

    }

}

您在请求开始后而不是在请求结束时重新加载 table,因此您的字典仍然包含上次查询的结果 运行。

self.dict = result

之后,将调用 getCountriesNamesFromServer 之后的 updateSearchResults.. 方法中的所有内容移动到网络请求的完成处理程序中

您的新方法是:

func updateSearchResultsForSearchController(searchController: UISearchController) {    
    let searchWord = searchController.searchBar.text!    
    getCountriesNamesFromServer(searchWord)        
}

func getCountriesNamesFromServer(searchWord:String) {        
    let url:String = "http://localhost"
    let params = ["keyword":searchWord]

    ServerRequest.postToServer(url, params: params) { result, error in    
        if let result = result {
            print(result)

            self.dict = result

            self.filteredKeys.removeAll()

            for (key, value) in self.dict {
                let valueContainsCity: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["city_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false

                let valueContainsCountry: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["country_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false

                if valueContainsCity || valueContainsCountry {
                    self.filteredKeys.append(key as! String)
                }
            }

            self.tableView.reloadData()   
        }               
    }
}