2015-06-07 43 views
0

我想通过UITableViewController之间的图像和其他数据(它具有自定义UITableViewCell)。进入功能prepareForSegue我做以下,但它不工作。如何在控制器之间传递数据?

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 
     } 
    } 
} 
+2

定义“不工作” FYI - 这些都是有用的至少一个的话开发者可以你需要清楚应该发生什么和实际发生的情况,并且更新你的问题并提供相关的细节 – rmaddy

回答

2

您正在调用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 
     } 
    } 
} 
+0

谢谢你。 – Alexander

0

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

越来越一个'新'单元(或者它可以重复使用一个已存在的单元),而不是从你的tableview中获取选中的单元格。

你不应该使用单元格作为数据模型的替代方案 - 它们是sim一瞥您的数据。一旦检索到所选单元格的索引路径,只需索引到您的数组或其他数据模型即可检索您分配给单元格的图像cellForRowAtIndexPath

相关问题