2017-08-21 109 views
0

我有一個tableview,它列出了firebase中所有的「地方」。我有一個UISearchController顯然搜索這些「地方」。問題是,當我點擊UISearchController但沒有輸入任何內容,並選擇一個「地點」我得到一個索引超出範圍錯誤。如果我打字或者沒有激活UISearchController,它會很好地工作。只是當它處於活動狀態而不輸入時,當我收到錯誤時。它拋出的錯誤「讓用戶= filteredUsers [indexPath.row]」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]這樣的:

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 
      } 
     } 
    } 
}