实现单单元格选择以在swift中显示细节视图
我正在使用集合视图,我在集合视图中显示了一系列图像,现在我需要在另一个视图中以更大的尺寸显示选定的图像。所以我实现如下 func collectionViewcollectionView:UICollectionView,numberOfItemsInSection:Int->Int{实现单单元格选择以在swift中显示细节视图,swift,uicollectionview,Swift,Uicollectionview,我正在使用集合视图,我在集合视图中显示了一系列图像,现在我需要在另一个视图中以更大的尺寸显示选定的图像。所以我实现如下 func collectionViewcollectionView:UICollectionView,numberOfItemsInSection:Int->Int{ return logoImage.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPat
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中的可选值时,我意外地发现错误为nil。
但是newCell.imageView.image仍然有一个值,我不知道为什么要面对这个错误。
有人能帮我解决这个错误吗。我通常会在情节提要中创建一个序列,当选中该序列时会转到细节视图,然后使用prepareForSegue,而不是使用didSelectItemAtIndexPath
至于你的具体问题,我不确定是什么原因导致它返回零,但我已经做了很多次你试图用这种方式做的事情,希望它也能为你工作。我今天遇到了同样的问题。 由于您的视图是在故事板中创建的,但您是通过编程方式调用它的,因此在调用时视图不会完全呈现 fullImageVw.imageFull.image=newCell.imageView.image 您的一个修复方法是在之前在线运行fullImageVw.view fullImageVw.imageFull.image=newCell.imageView.image 这样,fullImageVw.imageFull的imageView将不会为零,并且可以正常工作 希望能有帮助
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)
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)