2017-02-09 6 views
1

私は自分のデータベースで文字列検索をいくつか行いましたが、ユーザが検索バーの3番目の文字をプッシュした後にJSONを返した後、すべての結果を配列に追加しますテーブルビューにデータを埋め込みます。Alamofireリクエストでテーブルを更新するスウィフト検索バー

私は問題に取り組んでいますが、私が検索したとき、私は内部エラーindex out of rangeを取得し、私は私がすべてのテキストを削除し、私は女性、良いすべてを記述する場合の結果は、テーブルビュー

に表示されます取得...しかし、私はいくつかの文字列などを削除していたときにmensのために言うことができますテーブルビューcellForRowAt。私は、テーブルビューには、追加された配列を取得し、セルを埋める時間がないので、それが壊れていると思います。

私が調べてみたいことは、どのようにしてそれを動作させることができるかです。クエリの時間遅れやreloadData()を設定しないでください。私は時間の遅れを望まない理由は、一部のユーザーのiPhoneが遅くて2秒遅れてもクラッシュする可能性があるからです。

これはのtableViewはreloadDataをしながら、あなたはfilteredDataの状態を変更しているように見えます私のコード

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

です。 numberOfRowsForSectionが3を返し、ユーザーが削除キーを押すと、配列のサイズは0になります。cellForRowがインデックス0,1,2を要求するときに例外をスローする理由が考えられます。

関連する問題