简体   繁体   English

SWIFT:对TableView进行排序并将数据传递给第二个viewController

[英]SWIFT: sorting TableView and pass data to second viewController

I have very interesting problem: 我有一个非常有趣的问题:

I have two view controllers: FirstViewController and SecondViewController. 我有两个视图控制器:FirstViewController和SecondViewController。

There is table view in FirstViewController. FirstViewController中有表格视图。

There is a button to order rows in table. 有一个按钮可以对表中的行进行排序。

There is segue between view controllers. 视图控制器之间存在冲突。

//MARK: Sorting function:
func changeSorting(buttonTag: Int) {

        let button = self.view.viewWithTag(buttonTag) as? UIButton
        let buttonName = (button?.currentTitle)! as String

        //Apply Sorting to table:

            arrayOfBanks.sortInPlace {
                item1, item2 in

                let val1 = item1["sellRate"] as! Double
                let val2 = item2["sellRate"] as! Double

                if buttonName == "0-9⬇︎" {
                    return val1 > val2
                } else  {
                    return val2 > val1
                }
            }
         self.tableView.reloadData()

} }

    //MARK: Navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showCalculator" {
            let viewController = segue.destinationViewController as! SecondViewController
            let indexPath = tableView.indexPathForSelectedRow?.row

            print(indexPath!)

            viewController.bn = self.bankDict[indexPath!]["bankName"]! as! String
            viewController.cn = self.bankDict[indexPath!]["currencyName"]! as! String
            viewController.br = String(self.bankDict[indexPath!]["sellRate"]!)
            viewController.sr = String(self.bankDict[indexPath!]["buyRate"]!)

        }

Imagine that we have three rows in table with different bank names: Bank1, Bank2, Bank3. 想象一下,我们表中有三行具有不同的银行名称:Bank1,Bank2,Bank3。 Sorting functions works well - it updates my table. 排序功能运作良好-更新了我的表格。 Lets say after sorting we have new sequence: Bank3, Bank2, Bank1. 可以说,排序后,我们有了新的序列:Bank3,Bank2,Bank1。

So after all - the problem is when I use segue to export values to secondViewController. 所以毕竟-问题是当我使用segue将值导出到secondViewController时。 It works just fine if I don't use sorting. 如果我不使用排序,它就可以正常工作。 But If I do, it transfers values as it would be before sorting. 但是,如果这样做,它会像在排序之前一样传输值。

EG without sorting if I press top row it would export bank1. 没有排序的EG如果我按第一行,它将导出bank1。

but. 但。 after sorting we have Bank3 on the top row, but it still transfers Bank1 to secondViewController. 排序后,我们在第一行显示Bank3,但仍将Bank1转移到secondViewController。

Can anybody explain what I am missing? 谁能解释我所缺少的吗? Thanks. 谢谢。

It looks like in changeSorting you update an array called arrayOfBanks . 看起来在changeSorting您更新了一个名为arrayOfBanks的数组。 I assume this array is your data source. 我假设此数组是您的数据源。 But in prepareForSegue you use the index path of the selected row to access a dictionary called bankDict . 但是在prepareForSegue您使用所选行的索引路径来访问名为bankDict的字典。 This dictionary was not touched by the sorting method, so it's ordering would be the same as before. 该字典没有被排序方法所影响,因此其排序与以前相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM