首页 > 解决方案 > 我们如何在集合视图中重新加载标题视图

问题描述

我一直在寻找一种方法来重新加载我的集合视图标题。所以我有一个集合视图标题和一个只包含图像的 CollectionViewCell。现在,当按下单元格时,我想在标题视图中显示图像而不调用 collectionView.reloadData()。这就是我的 didSelectItemAt 和 didDeselectItemAt 方法的样子。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    selectedImage = images[indexPath.item]
    let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
    imageCell.photoBackgroundView.backgroundColor = .red

    collectionView.reloadData()
}

override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let imageCell = collectionView.cellForItem(at: indexPath) as! ImagePickerCell
    imageCell.photoBackgroundView.backgroundColor = .black
}

因此,当我选择一个单元格时,视图变为红色,当我取消选择它时,视图变为黑色。此处的此视频显示了无需重新加载 collectionView 的行为。现在这里是我想重新加载标题视图。

如果我确实使用了 collectionView.reloadData(),这就是结果。我如何能够重新加载标题或 collectionView,其中标题视图显示选定的单元格图像并变为红色。

标签: swiftuicollectionview

解决方案


You can try like global instance for that. Like

class YourClass: UIViewController {

 /// Profile imageView
    var profileImageview = UIImageView()

}

In CollectionView cellforItem assign a imageview. Like

let imageCell = collectionView.cellForItem(at: indexPath) as! CollectionCell
profileImageview = imageCell.imageView

Then when every you selecting collectionViewCell

You can call a function to change a image of imageView. Like

func updateImage() {
  profileImageview.image = UIImage()

}

推荐阅读