【问题标题】:Swift - segue from custom cell and cell data passing to another controllerSwift - 从自定义单元格和单元格数据传递到另一个控制器
【发布时间】:2015-03-14 12:07:53
【问题描述】:

在尝试将数据从 tableview 自定义单元格传递到另一个 viewcontroller 时,我被困了几个小时。

更准确地说:

  • TableViewController1 将 CustomCell1 作为 Prototype Cell 类

  • CustomCell1 有 Button1、Button2、Label1 和 Label2

  • ViewController2 有 Label3

TableViewController1 填充良好,有 10-20 行数据,所有内容都在屏幕上正常显示。

当用户点击 Button1 或 Button2 时(据我所知,这会触发按钮的 IBAction 而不是选择行)我以编程方式从添加到 TableViewController1 的按钮的 IBAction 触发一个 segue。

我想不通的是当用户点击 Button1 时如何传递 Label1 文本,当用户点击 Button2 到 ViewController2 时如何传递 Label2 文本并在 Label3 中显示适当的文本。

由于 tableview 中没有选择行,如果可能的话,如何做到这一点?

谢谢!

科斯塔斯

【问题讨论】:

  • 有一种既快速又便宜的方法 - 为按钮设置标签以根据行索引,例如 1001、1002 - 用于 button1 和 2001、2002.. 等用于 button2

标签: swift tableview segue tableviewcell


【解决方案1】:

确保您已正确准备好转场。这是我第一次尝试时的挂断。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "showTea"){
        let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
        let genView:genViewController = segue.destinationViewController as! genViewController
        genView.row = selectedIndexPath.row

    }
    else if (segue.identifier == "addTea"){

    }
}

然后在我的 genViewController 中,我添加了 var 行来保存该行的值,因此当我在 genViewController 中提取数据时,它会提取正确的数据。

var row: Int = 0

请参阅我关于此主题的问题,希望对您有所帮助。 core data swift sugue not pushing data to second view controller

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多