【问题标题】:Swift Firebase UISearchController Index Out Of RangeSwift Firebase UISearchController 索引超出范围
【发布时间】:2017-08-21 22:00:22
【问题描述】:

我有一个表格视图,列出了我在 firebase 中的所有“地点”。我有一个 UISearchController 显然可以搜索这些“地方”。问题是当我只是点击 UISearchController 但不输入任何内容并选择“位置”时,我得到一个索引超出范围错误。如果我正在输入或没有激活 UISearchController,它会很好。就在它处于活动状态且不输入时,我收到错误消息。它在“让用户=过滤用户[indexPath.row]”上引发错误

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

【问题讨论】:

  • 添加searchbar.text == ""条件,如果我正确地解决了您的问题
  • 我应该在哪里添加?
  • 感谢您的回答

标签: swift firebase search segue uisearchcontroller


【解决方案1】:

正如你所说,你没有执行任何搜索并选择一个地方,对吧?如果是这样,你调用空的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]

【讨论】:

    【解决方案2】:

    刚刚添加了这个“&& 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
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 2018-05-13
      • 1970-01-01
      • 2016-11-10
      • 2017-01-05
      • 1970-01-01
      相关资源
      最近更新 更多