Swift TableView didSelectRow无法按预期结果执行

Swift TableView didSelectRow无法按预期结果执行,swift,realm,tableview,segue,didselectrowatindexpath,Swift,Realm,Tableview,Segue,Didselectrowatindexpath,根据显示的视频,我希望TransactionListViewController将从AccountViewController中选择的单元格显示为选中 但是,TransactionViewController仅显示第0节第0行和第0节第1行,而其他节和第1行继续显示第0节第1行 预期结果 第0节第0行将对第0节第0行执行segue和pull数据 第0节第1行将为第0节第1行执行segue和pull数据 第1节第0行将对第1节第0行执行segue和pull数据 第1节第1行将为第1节第1行执行se

根据显示的视频,我希望TransactionListViewController将从AccountViewController中选择的单元格显示为选中

但是,TransactionViewController仅显示第0节第0行和第0节第1行,而其他节和第1行继续显示第0节第1行

预期结果

第0节第0行将对第0节第0行执行segue和pull数据

第0节第1行将为第0节第1行执行segue和pull数据

第1节第0行将对第1节第0行执行segue和pull数据

第1节第1行将为第1节第1行执行segue和pull数据

第2节第0行将对第2节第0行执行segue和pull数据

第2节第1行将为第2节第1行执行segue和pull数据


为什么只使用
作为
发送方
而不是整个
索引XPath
?然后你就有了完成任务所需的所有信息。对我来说,您应该使用model而不是
indepath
来执行segue。这段代码很容易出错。 此外,您还可以在大多数地方使用switch而不是
if-else

第二个屏幕如何知道您在第一个屏幕中选择了什么?您必须重新考虑您的实现。在第二个屏幕中,不从第一个屏幕开始对节索引进行操作。只需发送您想要显示的数据模型,然后从那里开始


此外,您不会将以前设置的值取消设置为'self.selectedPA'和其他“selected”值。

调用segue时,您只传递行
performsgue(带标识符:
SegToTransactionPage),发送方:indexPath.row)`每次。它不知道哪个节是,因此它总是0。您还需要告诉它该节是什么
indexPath.section
#AccountTableViewController

 var accTypePA : Results<AccTypePA>?
 var accTypeCA : Results<AccTypeCA>?
 var accTypeInv : Results<AccTypeInv>?
 var accTypeAss : Results<AccTypeAss>?
 var accTypeLbty : Results<AccTypeLbty>?

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        if indexPath.section == 0 {
            let strAccountPA = accTypePA
            selectedPA = strAccountPA?[indexPath.row]
            performSegue(withIdentifier: "SegToTransactionPage", sender: indexPath.row)

        } else if indexPath.section == 1 {
            let strAccountCA = accTypeCA
            selectedCA = strAccountCA?[indexPath.row]
            performSegue(withIdentifier: "SegToTransactionPage", sender: indexPath.row)

        } else if indexPath.section == 2 {
            let strAccountCA = accTypeCA
            selectedCA = strAccountCA?[indexPath.row]
            performSegue(withIdentifier: "SegToTransactionPage", sender: indexPath.row)

        } else if indexPath.section == 3 {
            let strAccountCA = accTypeCA
            selectedCA = strAccountCA?[indexPath.row]
            performSegue(withIdentifier: "SegToTransactionPage", sender: indexPath.row)

        } else if indexPath.section == 4 {
            let strAccountCA = accTypeCA
            selectedCA = strAccountCA?[indexPath.row]
            performSegue(withIdentifier: "SegToTransactionPage", sender: indexPath.row)
        }

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "SegToTransactionPage") {
            if let transactionVC = segue.destination as? TransactionTableViewController {

                transactionVC.selectedPA = self.selectedPA
                transactionVC.selectedCA = self.selectedCA
                transactionVC.selectedInv = self.selectedInv
                transactionVC.selectedAss = self.selectedAss
                transactionVC.selectedLbty = self.selectedLbty

            }

        }
    }


# TransactionListTableViewController

var selectedPA : AccTypePA?
var selectedCA : AccTypeCA?
var selectedInv : AccTypeInv?
var selectedAss : AccTypeAss?
var selectedLbty : AccTypeLbty?


 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 0 {
            print("Selected section 0 : \(section)")
            return selectedPA?.transPAList.count ?? 0
        } else if section == 1 {
            print("Selected section 1 : \(section)")
            return selectedCA?.transCAList.count ?? 0
        } else if section == 2 {
        print("Selected section 2 : \(section)")
        return selectedCA?.transCAList.count ?? 0
        } else if section == 3 {
        print("Selected section 3 : \(section)")
        return selectedCA?.transCAList.count ?? 0
        } else if section == 4 {
        print("Selected section 4 : \(section)")
        return selectedCA?.transCAList.count ?? 0
        }
        return transactions?.count ?? 1
        }