2017-08-21 48 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 
      } 
     } 
    } 
}