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

Ios 滚动swift3时如何隐藏UICollectionView中第0行的锁图像,ios,swift3,scroll,uicollectionview,Ios,Swift3,Scroll,Uicollectionview,我有一个UICollectionView,它工作得很好。我在除第0行之外的所有行上添加了锁定图像。加载ViewController时,它工作正常,但当我水平滚动它时,它在第0行显示锁定图像。我做错了什么?谢谢你 这是我的密码:- var imageView1 = UIImageView() func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICol

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

如果要将其直接添加到单元格中,则需要从其superview中删除,因为
imageView1
没有上一个单元格的引用,因此我们将使用单元格重用。 您可以使用上述解决方案,也可以在自定义单元格中添加锁定图像,并在中维护隐藏/显示


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

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

这对我来说是可行的,我在序列图像板的图像视图中添加了一个小图像,然后使其最初隐藏,然后将锁定图像放在其上,这是可行的

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
中添加锁定图像视图,然后从
cellForItemAt
中隐藏/取消隐藏它。我想你的单元格是在情节提要或nib中设计的

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

中添加视图。请显示您的代码好吗?添加的代码伙伴。为什么要在UIImageView中添加子视图UIImageView?实际上我在所有行中都有图像,这就是为什么我要在锁定图标图像的情况下添加子视图的原因在imageview上。请参阅答案将imageview1代码放在其他单元格内,并在单元格内添加imageview try可能是它的工作方式。当我滚动时出现问题。我还在if块中添加了imageview1.removeFromSuperview()。它不起作用。您是否尝试在自定义单元格中添加图像创建图像对象,如
cell.imageview
if(indexath.row==0){cell.lockImage.ishiden=true}否则{cell.lockImage.ishiden=false}
Buddy第一次加载视图时,当我滚动时,它是隐藏的,而不是o位置锁定图像。这是我的问题。是的,它应该可以,但在您的情况下不起作用,所以请尝试另一种方法。感谢Buddy,它现在起作用了。我在故事板的图像视图中添加了小图像,然后使其最初隐藏,然后将锁定图像放入im随着年龄的增长,它正在发挥作用。@ChetanLodhi Happy Coding:)