【问题标题】:Autocomplete Search List issue In SwiftSwift中的自动完成搜索列表问题
【发布时间】:2016-03-02 20:59:36
【问题描述】:

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

            }
        }

    }

}

【问题讨论】:

    标签: ios swift uisearchcontroller uisearchresultscontroller


    【解决方案1】:

    您是在请求开始后而不是完成时重新加载表,因此您的 dict 仍然具有它运行的最后一个查询的结果。

    在您调用 getCountriesNamesFromServer 之后,将您的 updateSearchResults.. 方法中的所有内容移动到您的网络请求的完成处理程序中,然后再执行 self.dict = result

    你的新方法是:

    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()   
            }               
        }
    }
    

    【讨论】:

    • 您能否编辑我的代码。我是新手,所以不知道现在该怎么办。非常感谢
    • 我在答案中添加了一些代码,您基本上只需将该代码块从一种方法剪切并粘贴到另一种方法
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多