【问题标题】:TableView didSelectRow could not perform as expected resultTableView didSelectRow 无法按预期结果执行
【发布时间】:2020-04-05 10:14:31
【问题描述】:

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

但是,TransactionViewController 仅显示 Section 0 Row 0 和 Section 0 Row 1,而其他部分和行继续显示 Section 0 Row 1

预期结果

Section 0, Row 0 将为 Section 0, Row 0 执行 segue 和 pull 数据

Section 0, Row 1 将为 Section 0, Row 1 执行 segue 和拉取数据

Section 1, Row 0 将为 Section 1, Row 0 执行 segue 和 pull 数据

Section 1, Row 1 将为 Section 1, Row 1 执行 segue 和拉取数据

第 2 节第 0 行将为第 2 节第 0 行执行 segue 和拉取数据

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

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

【问题讨论】:

  • 调用 segue 时,每次只传递行 performSegue(withIdentifier: SegToTransactionPage", sender: indexPath.row)`。它不知道是哪个部分,因此它始终为 0 . 你还需要告诉它是什么部分indexPath.section

标签: swift realm tableview segue didselectrowatindexpath


【解决方案1】:

为什么你只使用row 作为sender 而不是整个indexPath?然后你就有了你需要做的所有信息。对我来说,你应该使用模型而不是 indexPath 来做 segue。这段代码很容易出错。 也可以在大多数地方使用 switch 代替if else

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

另外,您不要取消将之前设置的值设置为 `self.selectedPA' 和其他“选定”值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多