如何将tableview单元格图像传输到详细视图,例如我想在用户单击单元格时在另一个视图控制器中显示单元格图像
我已经搜索但没有得到完美答案......
谢谢..这是我的代码
来源viewController
var tweakImage: UIImage! //here I have stored the cell Image
override func prepareForSegue….{
detailVC.tweakDetailImage = tweakImage // here i am sending it to another view
}
目标视图控制器
@IBOutlet weak var tweakImageView: UIImageView! // image view in destination view controller where i have to set the image
view didLoad{
tweakImageView.image = tweakDetailImage
}
答案 0 :(得分:1)
您可以在prepareForSegue
方法
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var row = tblViewTest.indexPathForSelectedRow()?.row
//Fetch object/value for the row selected from array or dictionary
// let say "objectAtIndex"
var newViewController = segue.destinationViewController
newViewController.objectSelected = objectAtIndex
}
确保在第二个视图控制器中创建名为objectSelected
的对象
if segue.identifier == "ImageScene" {
var row = tblViewTest.indexPathForSelectedRow()?.row
var imageName: String = "\(row!).jpg" //Unwrapping forcefully
var newViewController = segue.destinationViewController as! ImageViewController
newViewController.imageSelected = UIImage(named: imageName)
}
对imageName进行强制解包,因为我确定该行会有一个值,因为我在UITableView的didSelectRowAtIndex方法上执行了segue,现在我可以在第二个视图控制器上成功查看图像了
答案 1 :(得分:0)
您需要将datasource对象传递给detailController。必须有一些数据源用于在单元格上设置图像。
点击时,将其传递给您的详细控制器实例。