如何在UIView中显示所选UICollectioNViewCell的图像?(斯威夫特3)

Jes*_*ess 1 iphone ios swift

我是iOS开发的新手,所以如果这很明显,请耐心等待.

我在视图控制器的下半部分有一个UICollectionView; 它目前显示我照片库中的所有照片.在同一个视图控制器的上半部分有一个UIView.

问:当我选择UICollectionViewCell(即照片)时,如何在同一视图控制器上的UIView中显示该照片?

我想我必须以某种方式使用didSelectItemAt来保存所选图像的索引路径,以便在UIView中显示该图像.(到目前为止,我已经为didSelectItemAt包含了我的内容.)当选择不同的照片时,如何刷新UIView的内容?

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let selectedVideo = collectionView.cellForItem(at: indexPath)
    selectedVideo?.alpha = 0.5
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let deselectedVideo = collectionView.cellForItem(at: indexPath)
    deselectedVideo?.alpha = 1.0
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*mar 5

放入ImageView你的视图然后制作IBOutletimageview.你得到了集合视图的单元格,将图像collectionviewcellimageview图像设置为顶部imageView

你可以试试这个

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let selectedCell = collectionView.cellForItem(at: indexPath) as! MyCollectionViewCell
           yourview.imageView.image = selectedCell.imageView.image
        }
Run Code Online (Sandbox Code Playgroud)