UIImage没有出现

时间:2016-02-15 15:18:37

标签: ios swift uitableview uiimageview segue

当我从我的UIImageView中删除NewsTableViewController.swift时,图片不会显示在我的NewsDetailTableViewController.swift中。

这是我的模拟器的图像:

Simulator image[1]

以下是我Main.storyboard的图片:

Main.storyboard image[2]

这是我的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。

1 个答案:

答案 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
}