2017-02-09 55 views
1

我已經在我的數據庫中進行了一些字符串搜索,並且在用戶在搜索欄上按下第三個字符後返回JSON,之後我將所有結果附加到數組並用數據填充表格視圖。快速搜索欄 - 使用Alamofire請求更新表格視圖

雖然我正在處理一個問題。當我搜索讓說的mens我得到的結果將出現在tableview中

如果我刪除所有的文字和我寫的女性,都好......但是當我刪除某些字符串等我進去的錯誤index out of range tableview cellForRowAt。我想它中斷會導致tableview沒有時間來獲取附加數組並填充單元格。

我想知道的是我怎樣才能使它工作,沒有在查詢或reloadData()上設置時間延遲。我不想要時間延遲的原因是因爲某些用戶可能使用的是舊版iPhone,即使延遲了2秒,也可能會崩潰。

這是我的代碼

class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchControllerDelegate{ 

@IBOutlet weak var searchResultTable: UITableView! 
    let searchController = UISearchController(searchResultsController: nil) 

    var filteredData = [Products]() 

override func viewDidLoad() { 
     super.viewDidLoad() 


     self.navigationController?.isNavigationBarHidden = true 
     searchController.searchResultsUpdater = self 
     searchController.dimsBackgroundDuringPresentation = false 
     definesPresentationContext = true 
     searchResultTable.tableHeaderView = searchController.searchBar 

     searchController.searchBar.delegate = self 

    } 


func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 
     searchBar.resignFirstResponder() 
    } 


    func getSearch(completed: @escaping DownloadComplete, searchString: String) { 
     if filteredData.isEmpty == false { 
      filteredData.removeAll() 
     } 
     let parameters: Parameters = [ 
      "action" : "search", 
      "subaction" : "get", 
      "product_name" : searchString, 
      "limit" : "0,30" 
     ] 


     Alamofire.request(baseurl, method: .get, parameters: parameters).responseJSON { (responseData) -> Void in 
      if((responseData.result.value) != nil) { 
       let result = responseData.result 

       if let dict = result.value as? Dictionary<String, AnyObject>{ 
        if let list = dict["product_details"] as? [Dictionary<String, AnyObject>] { 

         for obj in list { 
          let manPerfumes = Products(productDict: obj) 
          self.filteredData.append(manPerfumes) 
         } 


        } 
       } 
       completed() 
      } 
     } 
    } 


func numberOfSections(in tableView: UITableView) -> Int { 

     return 1 

    } 
    func tableView(_ tableView: UITableView, 
        heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 170 
    } 

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

     return filteredData.count 
    } 

    func tableView(_ tableView: UITableView, 
        cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

      let cell:iPhoneTableViewCell = tableView.dequeueReusableCell(withIdentifier: "iPhoneTableCell", for: indexPath) as! iPhoneTableViewCell 

      let prods = self.filteredData[indexPath.row] //HERE I GET THE ERROR 
      cell.configureCell(products: prods) 

      return cell 

    } 

} 

extension SearchViewController: UISearchResultsUpdating { 

    func updateSearchResults(for searchController: UISearchController) { 

     if (searchController.searchBar.text?.characters.count)! >= 3 { 
        self.getSearch(completed: { 
        self.searchResultTable.reloadData() 

        self.searchResultTable.setContentOffset(CGPoint.zero, animated: true) 
       }, searchString: searchController.searchBar.text!) 
     } else { 
      self.searchResultTable.reloadData() 
     } 


    } 
} 

回答

1

它看起來像你改變filteredData的狀態,而做的tableView reloadData。如果numberOfRowsForSection返回3,然後用戶命中刪除鍵,則數組大小將變爲0.這可能是爲什麼cellForRow在請求索引0,1或2時拋出異常的原因。