我希望imageView图像等于CollectionView图像何时点击?

时间:2018-06-21 00:08:03

标签: swift uicollectionview

我有一个CollectionView,在下面的每个名为imageCell的单元格中都有一个随机图像。我还有另一个名为mainImage的imageView。我希望mainImage等于轻按imageCell时在CollectionView中选择的图像。我的尝试确实使imageCell等于mainImage图像,但是这样做确实很慢,并且只会执行一次。还有其他解决方案或解决方案吗?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCell

    cell.imageCell.image = UIImage(named: array[indexPath.row] + ".jpg")

    // This is my attempt below 
    if cell.isSelected {
       mainImage.image = cell.imageCell.image
    }

    return cell
}

1 个答案:

答案 0 :(得分:0)

您应该改用collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath),因为每次用户对collectionView的索引进行选择时都会调用它。因此,请尝试以下操作:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
     let image = UIImage(named: array[indexPath.row] + ".jpg")
     mainImage.image = image
}