2017-08-21 7 views
0

私はfirebaseからのすべての "場所"を一覧表示するテーブルビューを持っています。私は明らかにこれらの "場所"を検索するUISearchControllerを持っています。問題は、私がUISearchControllerをタップするだけですが、何も入力せずに範囲外エラーのインデックスを取得する「場所」を選択しないと発生します。私が入力している、またはUISearchControllerをアクティブ化していない場合、それはうまくいっています。それがアクティブで、入力しないときはエラーが発生したときです。それはあなたが言うように、あなたは右、任意の検索を実行し、場所を選択しなかったSwift Firebase UISearchControllerインデックスが範囲外にある

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    super.prepare(for: segue, sender: sender) 
    if segue.identifier == "BusinessProfiles" { 
     // gotta check if we're currently searching 
     if self.searchController.isActive { 
      if let indexPath = tableView.indexPathForSelectedRow { 
       let user = filteredUsers[indexPath.row] 
       let controller = segue.destination as? BusinessProfilesViewController 
       controller?.otherUser = user 
      } 
     } else { 
      if let indexPath = tableView.indexPathForSelectedRow { 
       let user = usersArray[indexPath.row] 
       let controller = segue.destination as? BusinessProfilesViewController 
       controller?.otherUser = user 
      } 
     } 
    } 
} 
+0

「searchbar.text == "" '条件を追加してください。正しく問題がある場合は – JuicyFruit

+0

どこに追加すればよいですか? –

+0

答えをありがとう –

答えて

0

「ユーザー= filteredUsers [indexPath.row]みましょう」のエラーがスローされますか?その場合は、選択された行のindexPath.rowで空のインデックス番号filteredUsers[indexPath.row]を呼び出します。検索が実行した場合ので、あなたは最初にチェックする必要があり、その後にのみ、このようfilteredUsers[indexPath.row]を呼び出す:

if !filteredUsers.isEmpty { 
    if self.searchController.isActive { 
      if let indexPath = tableView.indexPathForSelectedRow { 
       let user = filteredUsers[indexPath.row] 
0

ちょうど私の問題

を修正するために、この「& & searchController.searchBar.text =! 『』 」を追加します
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    super.prepare(for: segue, sender: sender) 
    if segue.identifier == "BusinessProfiles" { 
     // gotta check if we're currently searching 
     if self.searchController.isActive && searchController.searchBar.text != "" { 
      if let indexPath = tableView.indexPathForSelectedRow { 
       let user = filteredUsers[indexPath.row] 
       let controller = segue.destination as? BusinessProfilesViewController 
       controller?.otherUser = user 
      } 
     } else { 
      if let indexPath = tableView.indexPathForSelectedRow { 
       let user = usersArray[indexPath.row] 
       let controller = segue.destination as? BusinessProfilesViewController 
       controller?.otherUser = user 
      } 
     } 
    } 
} 
関連する問題