Ios 如何在控制器之间传递数据?

Ios 如何在控制器之间传递数据?,ios,uitableview,swift,segue,Ios,Uitableview,Swift,Segue,我想在UITableViewController之间传递图像和其他数据(它有自定义的UITableViewCell)。在函数prepareforsgue中,我做了以下操作,但它不起作用 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "identifierDetail" { if let index = self

我想在
UITableViewController
之间传递图像和其他数据(它有自定义的
UITableViewCell)
。在函数
prepareforsgue
中,我做了以下操作,但它不起作用

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "identifierDetail" {
        if let index = self.tableView.indexPathForSelectedRow() {
            let controller = (segue.destinationViewController as? UINavigationController)?.topViewController as? DetailViewController

            let cellIndentifier: String = "NewsCell"

            var cell: ParseTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIndentifier) as? ParseTableViewCell

            controller?.image = cell?.imageViewCell.image
        }
    }
}
这个

正在获取“新”单元格(或者可能正在重用现有单元格),而不是从tableview获取所选单元格


您不应该将单元格用作数据模型的替代品-它们只是数据的视图。检索所选单元格的indexPath后,只需索引到数组或其他数据模型中,即可检索分配给
cellForRowAtIndexPath

中单元格的图像,您正在调用
dequeueReusableCellWithIdentifier
,这将为您提供一个新单元格,如果要访问该单元格中的值,需要访问相关单元格的数据源,如下代码所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "identifierDetail" {
        if let index = self.tableView.indexPathForSelectedRow() {
            let controller = (segue.destinationViewController as? UINavigationController)?.topViewController as? DetailViewController

            let selectedData = dataSource[selectedIndexPath.row] //here dataSource is here the data to populate your table come from

            controller?.image = cell?.imageViewCell.image
        }
    }
}
定义“不起作用”。仅供参考-这些是开发人员能说的最没有帮助的词。你需要清楚应该发生什么和实际发生了什么。用相关细节更新你的问题。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "identifierDetail" {
        if let index = self.tableView.indexPathForSelectedRow() {
            let controller = (segue.destinationViewController as? UINavigationController)?.topViewController as? DetailViewController

            let selectedData = dataSource[selectedIndexPath.row] //here dataSource is here the data to populate your table come from

            controller?.image = cell?.imageViewCell.image
        }
    }
}