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

问题描述 投票:0回答:4

我有一个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
}
ios swift3 scroll uicollectionview
4个回答
1
投票

您在这里使用自定义集合视图单元格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
}

0
投票

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

如果要将其直接添加到单元格中,则需要从其超级视图中删除,因为imageView1没有前一个单元格的引用,我们使用单元格重用。对于您可以使用上述解决方案,或者您需要为自定义Cell添加锁定图像并保持隐藏/显示:

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


0
投票

您应该在故事板中添加锁定图像视图,但如果您想从代码中添加它,您应该在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中添加视图。


0
投票

这对我来说很有用,我在故事板上的图像视图中添加了小图像,而不是最初隐藏它,而不是将锁定图像图像放在上面,它正在工作。

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
       } 
© www.soinside.com 2019 - 2024. All rights reserved.