【问题标题】:How to hide lock image from row 0 in UICollectionView when Scrolling swift3滚动swift3时如何隐藏UICollectionView中第0行的锁定图像
【发布时间】:2018-06-01 21:38:07
【问题描述】:

我有一个 UICollectionView,它运行良好。我在除第 0 行之外的所有行上添加了锁定图像。加载 ViewController 时,它工作正常,但是当我水平滚动它时,它在第 0 行显示锁定图像。我做错了什么?提前致谢。

这是我的代码:-

var imageView1 = UIImageView()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
    cell.label.text = tittle[indexPath.row]

    cell.imageView.image = UIImage(named : image[indexPath.row] )

    imageView1  = UIImageView(frame:CGRect(x :cell.frame.size.width / 2 - 30 ,y : 40, width : 30, height : 30));

    imageView1.image = UIImage(named: "lock.png")
    imageView1.image =  imageView1.image!.withRenderingMode(.alwaysTemplate)
    imageView1.tintColor = UIColor.white

    if (indexPath.row == 0) {
        imageView1.isHidden = true
        imageView1.removeFromSuperview()
    } else {
        cell.imageView.addSubview(imageView1)
        if (RemoteModel.sharedInstanceRemoteModel.purchased){
            imageView1.isHidden = true
        } else {
            imageView1.isHidden = false
        }
    }
    return cell
}

【问题讨论】:

  • 请出示您的代码好吗?
  • 添加了代码伙伴。
  • 为什么要在 UIImageView 中添加 Subview UIImageView?
  • 实际上我在所有行中都有图像,这就是为什么我在 imageview 上添加锁定图标图像时添加子视图。
  • 查看答案将您的 imageview1 代码放入 else 并在单元格中添加 imageview 尝试可能是它的工作。

标签: ios swift3 scroll uicollectionview


【解决方案1】:

这对我有用,我在情节提要的图像视图上添加了小图像,而不是最初隐藏它,而不是将锁定图像图像放在上面,它正在工作。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        cell.label.text = tittle[indexPath.row]

        cell.imageView.image = UIImage(named : image[indexPath.row] )

        imageView1  = UIImageView(frame:CGRect(x :cell.frame.size.width / 2 - 30 ,y : 40, width : 30, height : 30));

        cell.lockiconmindcultivation.isHidden = false
            cell.lockiconmindcultivation.image = UIImage(named: "lock.png")
            cell.lockiconmindcultivation.image =  cell.lockiconmindcultivation.image!.withRenderingMode(.alwaysTemplate)
            cell.lockiconmindcultivation.tintColor = UIColor.white

        if (indexPath.row == 0){

            cell.lockiconmindcultivation.isHidden = true
            }

            cell.lockiconmindcultivation.isHidden = false
            cell.lockiconmindcultivation.image = UIImage(named: "lock.png")
            cell.lockiconmindcultivation.image =  cell.lockiconmindcultivation.image!.withRenderingMode(.alwaysTemplate)
            cell.lockiconmindcultivation.tintColor = UIColor.white

            if (RemoteModel.sharedInstanceRemoteModel.purchased){
                cell.lockiconmindcultivation.isHidden = true
            }else{
                cell.lockiconmindcultivation.isHidden = false
            }
        }

        return cell
       } 

【讨论】:

    【解决方案2】:

    您应该在情节提要中添加锁定图像视图,但如果您想从代码中添加它,您应该在 CollectionViewCell 中添加锁定图像视图,然后在 cellForItemAt 中隐藏/取消隐藏它。我想你的单元格是用故事板或笔尖设计的。

    class CollectionViewCell : UICollectionViewCell {
        var lockImageView: UIImageView?
        override func awakeFromNib() {
            lockImageView = UIImageView(frame:CGRect(x :self.frame.size.width / 2 - 30 ,y : 40, width : 30, height : 30));
            lockImageView?.image = UIImage(named: "lock.png")!.withRenderingMode(.alwaysTemplate)
            lockImageView.tintColor = UIColor.white
            self.contentView.addSubview(lockImageView!)
        }
    }
    

    cellForItemAt 中只需隐藏/取消隐藏。

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        cell.label.text = tittle[indexPath.row]
        cell.imageView.image = UIImage(named : image[indexPath.row] )
    
        if (indexPath.row == 0) {
            cell.lockImageView.isHidden = true
        } else {
            if (RemoteModel.sharedInstanceRemoteModel.purchased){
                cell.lockImageView.isHidden = true
            } else {
                cell.lockImageView.isHidden = false
            }
        }
        return cell
    }
    

    cellForItemAt 方法中添加和删除视图效率不高。而且你不应该在UIImageView 中添加视图。

    【讨论】:

      【解决方案3】:

      这里您使用的是自定义集合视图单元,即 CollectionViewCell

      因此,请尝试将其添加到您设计的自定义单元 xib 或故事板原型单元中

      然后将其连接到插座

      然后尝试根据条件隐藏/取消隐藏该图像视图

      最初尝试将其隐藏在 xib 或故事板中

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
      
          cell.label.text = tittle[indexPath.row]
      
          cell.imageView.image = UIImage(named : image[indexPath.row] )
      
          if (indexPath.item == 0)
          {
              cell.imageView1.isHidden = true
          }
          else
          {
              if (RemoteModel.sharedInstanceRemoteModel.purchased)
              {
                  cell.imageView1.isHidden = true
              }
              else
              {
                  cell.imageView1.isHidden = false
              }
          }
          return cell
      }
      

      【讨论】:

      • 是的,但它在你的情况下不起作用,所以尝试另一个方法
      • 谢谢哥们,它现在正在工作。我在情节提要的图像视图上添加了小图像,而不是最初隐藏它,而不是将锁定图像图像放在上面,它正在工作。
      • @ChetanLodhi 快乐编码 :)
      【解决方案4】:

      您正在使用此方法 cell.imageView.addSubview(imageView1) 在cell.imageView 中添加 UIImageView imageView1

      如果您将其直接添加到需要从其父视图中删除的单元格中,因为imageView1 没有来自前一个单元格的引用,我们正在使用单元格重用。 对于要么您可以使用上述解决方案,要么您需要在自定义单元格中添加锁定图像并在 hide/show 中维护:

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

      【讨论】:

      • 滚动时出现问题。我还在 if 块中添加了 imageView1.removeFromSuperview()。它不起作用。
      • 您是否尝试过在自定义单元格中添加图像创建图像对象,例如cell.imageViewif (indexPath.row == 0) { cell.lockImage.isHidden = true } else { cell.lockImage.isHidden = false }
      • 哥们,当我第一次加载视图时,当我滚动时,它是隐藏的,而不是位置锁定图像即将到来。这是我的问题。
      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 2022-07-23
      相关资源
      最近更新 更多