簡體   English   中英

Swift中的自動填充搜索列表問題

[英]Autocomplete Search List issue In Swift

您好,我的搜索功能遇到一個非常奇怪的問題。 我已經成功實現了搜索功能。 我正在從后端服務獲取數據。 問題是在某個階段,根據鍵入的關鍵字,數據無法在建議區域(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

            }
        }

    }

}

您將在請求開始后而不是完成時重新加載表,因此您的字典仍然具有上一次運行查詢的結果。

在執行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()   
        }               
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM