滚动swift3时如何隐藏UICollectionView中第0行的锁定图像

How to hide lock image from row 0 in UICollectionView when Scrolling swift3

我有一个 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
}

您正在使用此方法 cell.imageView.addSubview(imageView1)

cell.imageView 中添加 UIImageView imageView1

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

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

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

因此请尝试将其添加到自定义单元格 xib 或故事板原型单元格中,无论您在哪里设计它

然后连接到插座

然后根据条件

尝试 hiding/unhiding 该图像视图

并且最初尝试将其隐藏在 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
}

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

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
       } 

您应该在情节提要中添加锁定图像视图,但如果您想从代码中添加它,您应该在 CollectionViewCell 中添加锁定图像视图,而只是 hide/unhide 来自 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 中只是 hide/unhide。

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 中添加视图。