2016-03-02 23 views
0

您好,我的搜索功能中有一個非常奇怪的問題。我已成功實施搜索功能。我從後端服務獲取數據。問題是在某個階段,根據鍵入的關鍵字,數據無法在建議區域(tableView)中準確加載。我也將結果打印在控制檯上,以檢查我是否獲得了關鍵字的準確結果,並且控制檯顯示準確的結果,只是建議區域不會在某個時間加載準確的結果。例如在我的應用程序如果我想搜索城市「拉合爾」。我輸入完整的字母「拉合爾」自動完成搜索列表問題在Swift中

這表明該enter image description here

,但是當我按下X圖標或退格鍵刪除「E」,它顯示準確的結果

enter image description here

我只是展示它作爲一個例子。這幾乎是所有的時間都在發生。你可以看看我的代碼,看看我在做什麼錯。

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 

      } 
     } 

    } 

} 

回答

2

重新裝入您的表您的請求,而不是啓動它完成時後,讓你的字典仍然有從它跑的最後一個查詢的結果。

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

請您編輯我的代碼:

updateSearchResults..方法是調用getCountriesNamesFromServer到完成處理程序爲您的網絡請求後,你做self.dict = result

你的新方法將是後移的一切。我是新人,所以現在不知道該怎麼做。將非常感謝 – user1hjgjhgjhggjhg

+0

我給我的答案添加了一些代碼,你基本上只需要將該代碼塊從一種方法剪切並粘貼到另一種方法 – dan

+0

非常感謝... – user1hjgjhgjhggjhg