【问题标题】:How can we reload header view inside our collection view我们如何在集合视图中重新加载标题视图
【发布时间】:2018-11-26 05:39:00
【问题描述】:

我一直在寻找一种方法来重新加载我的集合视图标题。所以我有一个集合视图标题和一个只包含图像的 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
}

所以当我选择一个单元格时,视图变为红色,当我取消选择它时,视图变为黑色。这个video here,展示了不重新加载collectionView 的行为。现在这里是我想重新加载标题视图。

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

【问题讨论】:

    标签: swift uicollectionview


    【解决方案1】:

    您可以尝试使用全局实例。喜欢

    class YourClass: UIViewController {
    
     /// Profile imageView
        var profileImageview = UIImageView()
    
    }
    

    在 CollectionView cellforItem 中分配一个 imageview。喜欢

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

    然后当你每次选择collectionViewCell时

    您可以调用函数来更改 imageView 的图像。喜欢

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

    【讨论】:

      【解决方案2】:

      好吧,我试图理解为什么有时你将单元格投射到 CollectionCell 中,有时又投射到 ImagePickerCell 中,无论如何尝试更改这些函数中的函数

      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
          cell?.photoBackgroundView.backgroundColor = .red
      }
      
      func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
          let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
          cell?.photoBackgroundView.backgroundColor = .black
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 2012-08-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-29
        相关资源
        最近更新 更多