如何通过在CollectionView中选择一个单元格来更改图像 [英] how do I change an image by selecting a cell in CollectionView

查看:70
本文介绍了如何通过在CollectionView中选择一个单元格来更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在一个项目上,我的产品页面是这样的
< img src = https://i.stack.imgur.com/GKldh.jpg alt =在此处输入图片描述>

Currently I am working on a project and my product page is like this

此处正在使用 UIImageView 来显示大图像,而在下面,我正在使用 CollectionView 来滚动一组图像。当我从 CollectionViewCell 单击图像时,我想相应地更改大图像。由于这是我第一次使用这种功能,因此我一无所知。请任何人给我一些建议。谢谢。

Here I am using an UIImageView to show the large image and below that I am using a CollectionView to scroll a set of images. I want to change the large image accordingly when I click an image from the CollectionViewCell . As this is the first time I am using this kind of functionality, I am out of clue. Please anybody give me some suggestion. Thank you.

推荐答案

如果每个图像都在单独的UICollectionViewCell中,请使用

If every image is in a separate UICollectionViewCell, use

override func collectionView(_ collectionView: UICollectionView,
                             didSelectItemAt indexPath: IndexPath) {
    // here you know which item is selected by accessing indexPath.item property, for example:
    let selectedImage = yourImages[indexPath.item]
    bigImageView.image = selectedImage
}

方法,用于检测用户何时选择集合视图项。
为了使用此方法,您需要遵循 UICollectionViewDelegate 并设置 collectionView.delegate = self

method to detect when user selects a collection view item. In order to use this method you need to conform to UICollectionViewDelegate and set collectionView.delegate = self

这篇关于如何通过在CollectionView中选择一个单元格来更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆