【问题标题】:UITableViewCell Segue SOMETIMES connects to wrong controllerUITableViewCell Segue 有时连接到错误的控制器
【发布时间】:2018-03-18 17:29:46
【问题描述】:

我的 tableView 有一个非常奇怪的问题,有时您单击一个单元格并按照它应有的方式进行切换,但其他时候它会切换到一个随机的 detailViewController。 我有 3 个从包含 tableview 的 UIViewController 连接的 segue:

segues 以模态方式“呈现”detailViewController 并将自定义对象“place”传递给 detailViewController

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("PLACE SELECTED: SECTION \(indexPath.section) ROW \(indexPath.row) :: \(my_sections[indexPath.section])")
    self.selectedPlace = my_sections[indexPath.section].rows[indexPath.row]
    let buttons_count = self.selectedPlace!.buttons.count
    switch buttons_count {
    case 0:
        self.performSegue(withIdentifier: SegueIdentifier.NoButton.rawValue, sender: self.tableview.cellForRow(at: indexPath))
    case 1:
        self.performSegue(withIdentifier: SegueIdentifier.OneButton.rawValue, sender: self.tableview.cellForRow(at: indexPath))
    case 2:
        self.performSegue(withIdentifier: SegueIdentifier.TwoButton.rawValue, sender: self.tableview.cellForRow(at: indexPath))
    default:
        break
    }

}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (sender as? PlaceTableViewCell) != nil {
        if let indexPath = self.tableview.indexPathForSelectedRow {
            let place = self.my_sections[indexPath.section].rows[indexPath.row]
            navigationController?.view.backgroundColor = UIColor.clear

            switch(segue.identifier!) {

            case SegueIdentifier.NoButton.rawValue:
                assert(segue.destination.isKind(of: PlaceDetailsViewController.self))
                let vc = segue.destination as! PlaceDetailsViewController
                vc.place = place

            case SegueIdentifier.OneButton.rawValue:
                assert(segue.destination.isKind(of: OneButtonViewController.self))
                let vc = segue.destination as! OneButtonViewController
                vc.place = place

            case SegueIdentifier.TwoButton.rawValue:
                assert(segue.destination.isKind(of: TwoButtonViewController.self))
                let vc = segue.destination as! TwoButtonViewController
                vc.place = place

            default: break
            }
        }
    } 
}

place 对象有 place.buttons: [Button]

三个 detailViewController 几乎相同,只是按钮数量不同。

tableView 根据 place.buttons 的大小决定使用哪个 segue

有时 tableView 像正常一样工作,有时它会传递随机单元格。不知道为什么。

【问题讨论】:

  • prepare(for:)为什么又用选中的单元格来获取位置呢?它已经在self.selectedPlace 中,或者您可以简单地将位置作为sender 参数提供给performSegue
  • 好的,在遵循这里给出的大部分建议之后,我认为这是一个排序错误。不知何故 my_sections.rows 与表中列出的顺序不匹配!

标签: ios swift uitableview


【解决方案1】:

您可以简化您的prepare(for: 方法来解决此问题,如下所示:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    navigationController?.view.backgroundColor = UIColor.clear

    switch(segue.identifier!) {

    case SegueIdentifier.NoButton.rawValue:
        assert(segue.destination.isKind(of: PlaceDetailsViewController.self))
        let vc = segue.destination as! PlaceDetailsViewController
        vc.place = self.selectedPlace

    case SegueIdentifier.OneButton.rawValue:
        assert(segue.destination.isKind(of: OneButtonViewController.self))
        let vc = segue.destination as! OneButtonViewController
        vc.place = self.selectedPlace

    case SegueIdentifier.TwoButton.rawValue:
        assert(segue.destination.isKind(of: TwoButtonViewController.self))
        let vc = segue.destination as! TwoButtonViewController
        vc.place = self.selectedPlace

    default: break
    } 
}

【讨论】:

  • 如此真实。感谢您发现这一点。我不确定这是否一定会解决我的问题,因为就像问题所说的那样,它发生得如此随机。我所能说的是,它更有可能在当天晚些时候发生,而且我的数据依赖于 Google Places & Calendar API
  • 刚刚实施了您的更改,问题仍然存在。非常不确定为什么!在视图控制器之间附加 segues 是不是很糟糕(而不是从 tableviewcell 到视图控制器?)
【解决方案2】:

似乎my_sections 是数组。在访问它的项目之前尝试对其进行排序。我有一个类似的问题,当程序从持久存储中获取数据并且数据数组中的项目是无序的。

【讨论】:

  • 这个问题是否可能与我的某些数据依赖于 Google Places & Calendar API 这一事实有关?我只是问,因为我注意到我曾经超过了我的 Google Places API 查询限制,一旦发生这种情况,tableview 就会自行修复。如果您不这么认为,请忽略这个问题——我不想让它变得更加混乱。
  • 另外,我在 my_sections "didSet" 时对数组进行排序。我称这个函数为:return self.my_sections.sort(by: { $0.index < $1.index})
  • 你用行对子数组进行排序吗?也许self.my_sections.sort(by: { $0.index < $1.index}) 只对组进行排序,而不是对组中的行进行排序
  • Is it possible that this problem has anything to do with the fact that some of my data is reliant upon Google Places & Calendar API? 。 - Google API 是否返回排序的项目列表,并且您每次都以相同的顺序编码解析响应?
【解决方案3】:

我发现了问题,对花时间在这里的人感到抱歉,我没有为任何人提供足够的信息来解决这个问题。

我的 tableview 从

获取信息
var my_sections: [Section] = [] {
        didSet {
            return self.my_sections.sort(by: { $0.index < $1.index})
        }
}

我的“部分”模型如下所示:

struct Section: Ordered {
    var section: PTPlace.Section
    var rows: [PTPlace]
    var sorted_rows: [PTPlace] {
        return self.rows.sorted(by: { $0.open_status.rawValue > $1.open_status.rawValue })
    }
}

我注意到当行在部分内而不是在部分之间混合时出现了一些问题。

所以我在didSelectRowAtIndexPath 方法中添加了以下行:

    print("PLACE SELECTED: SECTION \(indexPath.section) ROW 
\(indexPath.row) :: \(my_sections[indexPath.section].rows[indexPath.row].name)")

我看到 tableView 的排列方式与行的排序方式非常不同,就好像有两个不同的数组一样。果然,这就是问题所在。

我的 tableView 正在视图中显示 sorted_rows 数组,同时继续

my_sections[indexPath.section].rows[indexPath.row] 而不是 my_section[indexPath.section].sorted_rows[indexPath.row]

问题解决了。愚蠢的错误。再次感谢所有帮助我简化代码的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多