当我从我的UIImageView
中删除NewsTableViewController.swift
时,图片不会显示在我的NewsDetailTableViewController.swift
中。
这是我的模拟器的图像:
以下是我Main.storyboard
的图片:
这是我的prepareForSegue()
方法代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! NewsDetailTableViewController
destinationController.item = items[indexPath.row]
let newsImage = UIImageView(image: UIImage(named: detailImages[indexPath.row]))
destinationController.image = newsImage
}
}
}
值得一提的是,我NewsTableViewController.swift
中的图像设置为0x0,而NewsDetailTableViewController.swift
中的图像设置为600x216。我不知道这是否重要。如果您需要任何代码,请随时询问。
仅供参考,我使用Swift。
答案 0 :(得分:1)
在prepareForSegue中,destinationController的IBOutlets尚未初始化。只需将detailImages传递给NewsDetailTableViewController上的另一个数组属性即可。将prepareForSegue更改为:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! NewsDetailTableViewController
destinationController.item = items[indexPath.row]
destinationController.imageNames = detailImages
}
}
}
然后在NewsTableViewController中添加以下代码:
var imageNames:[String]?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//replace with whatever identifier you're using
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.imageView?.image = self.imageNames[indexPath.row]?
return cell
}