【问题标题】:SWIFT: sorting TableView and pass data to second viewControllerSWIFT:对 TableView 进行排序并将数据传递给第二个 viewController
【发布时间】:2016-04-04 19:57:01
【问题描述】:

我有一个非常有趣的问题:

我有两个视图控制器:FirstViewController 和 SecondViewController。

FirstViewController 中有表格视图。

有一个按钮可以对表格中的行进行排序。

视图控制器之间存在segue。

//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"]!)

        }

假设我们在表中有三行具有不同的银行名称:Bank1、Bank2、Bank3。排序功能运行良好 - 它更新了我的表格。假设排序后我们有新的序列:Bank3、Bank2、Bank1。

毕竟 - 问题是当我使用 segue 将值导出到 secondViewController 时。如果我不使用排序,它工作得很好。但如果我这样做,它会像排序之前一样传输值。

例如如果我按顶行而不进行排序,它将导出bank1。

但是。排序后,我们在第一行有 Bank3,但它仍然将 Bank1 传输到 secondViewController。

谁能解释我错过了什么? 谢谢。

【问题讨论】:

    标签: ios swift uitableview segue


    【解决方案1】:

    看起来在changeSorting 中你更新了一个名为arrayOfBanks 的数组。我假设这个数组是你的数据源。但是在prepareForSegue 中,您使用所选行的索引路径来访问名为bankDict 的字典。这个字典没有被排序方法触及,所以它的排序和以前一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多