2017-12-11 15 views
-1

私はcontrlセグメントとUITableviewを持っています。選択したセグメントまたは実行された検索によって、テーブルがリロードされます。コントロールセグメントは正常に動作していますが、検索は機能しません。バーに何かを入力しようとすると、リロードだけが行われ、セルをクリックするとクラッシュします。誰かがここで私を助けることができる?どうもありがとう致命的なエラー:項目の範囲外の検索のインデックス

var auxiliar: [Shops]? = [Shops]() 
    var searchActive: Bool = false  
// shops category 
    var restuarnt = [Shops]() 
    var coffee = [Shops]() 
    var pharmacy = [Shops]() 
    var supermarket = [Shops]() 
    var home = [Shops]() 
    var clothes = [Shops]() 
    var allShops = [Shops]()  
    var shops = [Shops]() 

    var selectedIndex = 0 



s.valueChange = { index in 

      if index == 0 { 

       self.selectedIndex = 0 
       self.tableView.reloadData() 

      } 
      if index == 1 { 
       self.selectedIndex = 1 

       self.tableView.reloadData() 
      } 

      if index == 2 { 
       self.selectedIndex = 2 

      } 
      if index == 3 { 

       self.selectedIndex = 3 

      } 



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if selectedIndex == 0 { 
      return allShops.count 

     } 
     if selectedIndex == 1 { 

      return self.coffee.count 
     } 
     if selectedIndex == 2 { 

      return self.restuarnt.count 
     } 
     if selectedIndex == 3 { 

      return self.pharmacy.count 
     } 

     if searchActive { 
      return auxiliar!.count 
     }else{ 
      return allShops.count 

     } 

    } 


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


    if selectedIndex == 0 { 

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

     let entry = shops[indexPath.row] 

     cell.shopImage.hnk_setImage(from: URL(string: entry.Logo)) 
     cell.shopName.text = entry.shopname 
     cell.star.rating = Double(entry.rate) 
     cell.time.text = entry.time 


     cell.backgroundColor = UIColor(white: 1 , alpha : 0.5) 
     return cell 
    } 

    if selectedIndex == 1 { 

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

     let entry = coffee[indexPath.row] 

     cell.shopImage.hnk_setImage(from: URL(string: entry.Logo)) 
     cell.shopName.text = entry.shopname 
     cell.star.rating = Double(entry.rate) 
     cell.time.text = entry.time 


     cell.backgroundColor = UIColor(white: 1 , alpha : 0.5) 
     return cell 
    } 

    if selectedIndex == 2 { 

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

     let entry = restuarnt[indexPath.row] 

     cell.shopImage.hnk_setImage(from: URL(string: entry.Logo)) 
     cell.shopName.text = entry.shopname 
     cell.star.rating = Double(entry.rate) 
     cell.time.text = entry.time 


     cell.backgroundColor = UIColor(white: 1 , alpha : 0.5) 
     return cell 
    } 
    if selectedIndex == 3 { 

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

     let entry = pharmacy[indexPath.row] 

     cell.shopImage.hnk_setImage(from: URL(string: entry.Logo)) 
     cell.shopName.text = entry.shopname 
     cell.star.rating = Double(entry.rate) 
     cell.time.text = entry.time 


     cell.backgroundColor = UIColor(white: 1 , alpha : 0.5) 
     return cell 
    } 

    if searchActive 

    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell 
     let entry1 = auxiliar?[indexPath.row] 


     cell.shopImage.hnk_setImage(from: URL(string: (entry1?.Logo)!)) 
     cell.shopName.text = entry1?.shopname 

     cell.backgroundColor = UIColor(white: 1 , alpha : 0.5) 
     cell.time.text = entry1?.time 

     cell.star.rating = Double((entry1?.rate)!) 
     return cell 
    } 
    else { 


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

     let entry = shops[indexPath.row] 

     cell.shopImage.hnk_setImage(from: URL(string: entry.Logo)) 
     cell.shopName.text = entry.shopname 
     cell.star.rating = Double(entry.rate) 
     cell.time.text = entry.time 


     cell.backgroundColor = UIColor(white: 1 , alpha : 0.5) 
     return cell 

    } 
} 



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     tableView.deselectRow(at: indexPath, animated: true) 
     if selectedIndex == 1 { 
      let meal1 = coffee[indexPath.row] 


      guard (coffee.count) > indexPath.row else { 
       print("Index out of range") 
       return 
      } 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController 
      viewController.passedValue = (meal1.familiy_id) 
      viewController.name = (meal1.shopname) 

      print("\(meal1.familiy_id) im the search ") 
      view.animateRandom() 
      self.present(viewController, animated: true , completion: nil) 



     } 
     if selectedIndex == 0 { 
      let meal1 = shops[indexPath.row] 


      guard (shops.count) > indexPath.row else { 
       print("Index out of range") 
       return 
      } 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController 
      viewController.passedValue = (meal1.familiy_id) 
      viewController.name = (meal1.shopname) 

      print("\(meal1.familiy_id) im the search ") 
      view.animateRandom() 
      self.present(viewController, animated: true , completion: nil) 



     } 
     if selectedIndex == 2 { 
      let meal1 = restuarnt[indexPath.row] 


      guard (restuarnt.count) > indexPath.row else { 
       print("Index out of range") 
       return 
      } 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController 
      viewController.passedValue = (meal1.familiy_id) 
      viewController.name = (meal1.shopname) 

      print("\(meal1.familiy_id) im the search ") 
      view.animateRandom() 
      self.present(viewController, animated: true , completion: nil) 



     } 
     if selectedIndex == 3 { 
      let meal1 = pharmacy[indexPath.row] 


      guard (pharmacy.count) > indexPath.row else { 
       print("Index out of range") 
       return 
      } 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController 
      viewController.passedValue = (meal1.familiy_id) 
      viewController.name = (meal1.shopname) 

      print("\(meal1.familiy_id) im the search ") 
      view.animateRandom() 
      self.present(viewController, animated: true , completion: nil) 



     } 

     if searchActive { 
      let meal1 = auxiliar?[indexPath.row] 


      guard (auxiliar?.count)! > indexPath.row else { 
       print("Index out of range") 
       return 
      } 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController 
      viewController.passedValue = (meal1?.familiy_id)! 
      viewController.name = (meal1?.shopname)! 

      print("\(meal1?.familiy_id) im the search ") 
      view.animateRandom() 
      self.present(viewController, animated: true , completion: nil) 




     } else { 
      let meal = shops[indexPath.row] 


      guard shops.count > indexPath.row else { 
       print("Index out of range") 
       return 
      } 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController 
      viewController.passedValue = meal.familiy_id 
      viewController.name = meal.shopname 
      view.animateRandom() 
      self.present(viewController, animated: true , completion: nil) 


     } 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
     auxiliar = shops.filter { $0.shopname.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil} 
     if searchText == "" || searchBar.text == nil { 
      auxiliar = shops 
     } 
     tableView.reloadData() 
    } 

クラッシュがlet meal1 = auxiliar?[indexPath.row]

+1

どのコード行がエラーを投げていますか? [最小限の、完全で検証可能な例](https://stackoverflow.com/help/mcve)を入力してください。無関係ですが、 'else'ブランチのない' if'節ではなく、 'switch'文を使ってコードを読みやすくする必要があります。 –

+0

'let meal1 = auxiliar?[indexPath.row]' – leo0019

答えて

1

あなたは次の2つのステートメントに交換する必要があり、このラインで原因である:

let meal1 = auxiliar?[indexPath.row] 
guard (auxiliar?.count)! > indexPath.row else { 
    print("Index out of range") 
    return 
} 

それはなります添字を(試しても意味がありませんがif auxiliar.count <= indexPath.row)、サブスクリプトが実際に安全かどうかをチェックしてください。

それは次のようになります。

guard auxiliar.count > indexPath.row else { 
    print("Index out of range") 
    return 
} 
let meal1 = auxiliar[indexPath.row] 

また、var auxiliar: [Shops]? = [Shops]()は意味がありません。デフォルト値をauxiliarに設定した場合、それをOptionalと定義するのはなぜですか?単にauxiliarを非オプションとして宣言するだけでvar auxiliar = [Shops]()となります。

また、selectedIndexをリセットしないと簡単に問題を引き起こす可能性がある2つの異なる変数を確認するため、2つの異なる変数をチェックしているため、実装ではtableView(:numberOfRowsInSection:)を再訪する必要があります。 if searchActive部分。

また、あなたのコードをリファクタリングし、明確な条件なしelse支店を持つswitch文の代わりに、いくつかのifステートメントを使用する必要があります。

+0

あなたの答えに感謝します!私はそれを修正しましたが、今はクラッシュしていませんが、検索の結果は正しくありません。 – leo0019

+0

私は 'textDidChange'を私の質問に追加しました。 – leo0019

関連する問題