Ios UITableViewCell Segue有时连接到错误的控制器

Ios UITableViewCell Segue有时连接到错误的控制器,ios,swift,uitableview,Ios,Swift,Uitableview,我的tableView出现了一个非常奇怪的问题,有时候你点击一个单元格并按它应该的方式进行分段,但有时候它会切换到一个随机的detailViewController。 我有3个分段从包含tableview的UIViewController连接: 分段以模式显示detailViewController,并将自定义对象位置传递给detailViewController func tableView(_ tableView: UITableView, didSelectRowAt indexPath:

我的tableView出现了一个非常奇怪的问题,有时候你点击一个单元格并按它应该的方式进行分段,但有时候它会切换到一个随机的detailViewController。 我有3个分段从包含tableview的UIViewController连接:

分段以模式显示detailViewController,并将自定义对象位置传递给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:[按钮]

这三个DetailViewController几乎相同,只是它们有不同数量的按钮

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

有时tableView的工作方式与正常工作方式类似,而有时它会通过随机单元格。不确定原因。

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

您可以简化preparefor:方法来解决此问题,如下所示:

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

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

我的tableview从中获取信息

var my_sections: [Section] = [] {
        didSet {
            return self.my_sections.sort(by: { $0.index < $1.index})
        }
}
我注意到当行在部分内而不是在部分之间混淆时,出现了一些问题

因此,我在didSelectRowAtIndexPath方法中添加了以下行:

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

我的tableView在切换到时在视图中显示已排序的_行数组

my_节[indexPath.section]。行[indexPath.row] 而不是我的\u节[indexPath.section]。已排序的\u行[indexPath.row]


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

在准备中:为什么要使用选定的单元格再次获得位置?它已经在self.selectedPlace中,或者您可以简单地将place作为sender参数提供给performsgue。不知何故,我的_sections.rows与表中列出的顺序不匹配!真的。谢谢你发现了这一点。我不确定这是否一定能解决我的问题,因为正如问题所说,这是随机发生的。我所能说的是,这一天晚些时候发生的可能性比早些时候更大,而且我的数据依赖于Google Places&Calendar,因为我刚刚实施了您的更改,问题仍在继续。很不确定为什么!在视图控制器之间连接分段(而不是从tableviewcell连接到视图控制器)是否不好?这个问题是否可能与我的一些数据依赖于Google Places&Calendar API这一事实有关?我之所以问这个问题,是因为我注意到我曾经超过了我的GooglePlacesAPI查询限制,一旦发生这种情况,tableview就自行修复了。如果你不这么认为,忽略这个问题-我不想让它变得更混乱。另外,我会在设置_部分时对数组进行排序。我调用这个函数:return self.my_sections.sortby:{$0.index<$1.index}是否按行对子数组排序?可能self.my_sections.sortby:{$0.index<$1.index}只对组进行排序,而不是对组中的行进行排序。这个问题可能与我的一些数据依赖于Google Places&Calendar API这一事实有关吗?-GoogleAPI是否返回已排序的项目列表,并且每次都以相同的顺序编写解析响应?
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 })
    }
}
    print("PLACE SELECTED: SECTION \(indexPath.section) ROW 
\(indexPath.row) :: \(my_sections[indexPath.section].rows[indexPath.row].name)")