使用Collection View时,我在集合视图中显示了图像数组,现在我需要在更大尺寸的另一个视图中显示所选图像。所以我实现如下。
func collectionView(collectionView:UICollectionView,numberOfItemsInSection section:Int) - > Int {
return logoImage.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : PhotoCollection = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCells", forIndexPath: indexPath) as PhotoCollection
cell.imageView.image = logoImage[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var newCell :PhotoCollection = collectionView.cellForItemAtIndexPath(indexPath) as PhotoCollection
var fullImageVw : FullImageClass = self.storyboard?.instantiateViewControllerWithIdentifier("FullImage") as FullImageClass
fullImageVw.imageFull.image = newCell.imageView.image
self.navigationController?.pushViewController(fullImageVw, animated: true)
}
但是在解开fullImageVw.imageFull.image = newCell.imageView.image行中的Optional值时意外发现错误为nil。 但是newCell.imageView.image还有一个值我还不知道为什么会遇到这个错误。 任何人都可以帮我解决这个错误。
答案 0 :(得分:0)
我通常会在故事板中创建一个segue,在选中时会转到详细信息视图,然后使用didSelectItemAtIndexPath
而不是prepareForSegue
使用override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailSegue"){
let cell = sender as Cell
var destination = segue.destinationViewController as DetailViewController
destination.image = cell.imageView.image
}
}
{{1}}
就你的具体问题而言,我并不确定是什么导致它返回nil,但我基本上已经完成了你多次尝试这样做的方式并且它已经有效,所以希望它对你有用太
答案 1 :(得分:0)
我今天遇到了同样的问题。 由于您的视图是在故事板中创建的,但是您以编程方式调用它,因此在您调用时视图不会完全呈现 fullImageVw.imageFull.image = newCell.imageView.image
您可以在之前的线路上运行fullImageVw.view fullImageVw.imageFull.image = newCell.imageView.image 以这种方式,fullImageVw.imageFull的imageView将不会是nil,它将正常工作 希望它有所帮助
var newCell :PhotoCollection = collectionView.cellForItemAtIndexPath(indexPath) as PhotoCollection
var fullImageVw : FullImageClass = self.storyboard?.instantiateViewControllerWithIdentifier("FullImage") as FullImageClass
fullImageVw.view
fullImageVw.imageFull.image = newCell.imageView.image
self.navigationController?.pushViewController(fullImageVw, animated: true)