如何将数据从 UITableViewCell 传递到 ViewController?

How to pass data from UITableViewCell to ViewController?

我遵循了多个教程,并在此处尝试了很多答案,但我似乎无法弄清楚问题所在。

这是我的代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        println("You selected cell #\(indexPath.row)!")


        let indexPath = homeTimelineTableView.indexPathForSelectedRow()
        let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell

        productTitle = currentCell.productCellTitleLabel!.text

        println("The product title is \(productTitle)")


        self.performSegueWithIdentifier("timelineDetail", sender: self)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var titleLables: String!

        if (segue.identifier == "timelineDetail") {

            println("it works thus far!")

            let viewController = segue.destinationViewController as! ProductDetailViewController



            let selectedRow = homeTimelineTableView.indexPathForSelectedRow()!.row

            viewController.titleLabel?.text = productTitle


        }else {

            println("wtf is wrong with your code?!")
        }




    }

我没有收到任何错误。但是,当我转到 运行 我的应用程序时,我的产品标题仍然拒绝传递给 viewcontroller。 有什么建议么?谢谢

现阶段viewController.titleLabel为nil(你用的是?,所以没有报错)

正确的做法应该是:

  • 在 ProductDetailViewController
  • 中添加 var productTitle: String!
  • 不将 productTitle 传递给 UILabel,而是传递给 viewController.productTitle
  • 在 ProductDetailViewController 的 viewDidLoad 中设置 viewController.titleLabel?.text = self.productTitle

必须从 prepareForSegue 方法中当前选定的单元格分配 productTitle 变量。

检查下面的代码。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    var titleLables: String!

    if (segue.identifier == "timelineDetail") {

        println("it works thus far!")

        let viewController = segue.destinationViewController as! ProductDetailViewController

        let selectedRow = homeTimelineTableView.indexPathForSelectedRow()!.row

        let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell

        productTitle = currentCell.productCellTitleLabel!.text

        viewController.titleLabel?.text = productTitle
    } else {
        println("wtf is wrong with your code?!")
    }
}

最容易出问题的是这两行:

let indexPath = homeTimelineTableView.indexPathForSelectedRow()
let currentCell = homeTimelineTableView.cellForRowAtIndexPath(indexPath!) as! ProductTableViewCell

首先,func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 提供了已选择的 indexPath。不需要 let indexPath = ....

其次,不要加载单元格的值(returns nil,如果单元格可见并且 tableView.visibleCells() 会带来其他问题),而是使用您的数据源在选定的 indexPath.

总的来说可能是这样的:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // `indexPath` comes from the function parameters
    let item = model.itemAtIndexPath(indexPath)
    // the 'model'-part should be the same like in `cellForRow:indexPath`
    // your mission is only to get the item at the path, not the cell

    productTitle = item.title
    self.performSegueWithIdentifier("timelineDetail", sender: self)
}