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

标签 ios swift3 scroll uicollectionview

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

最佳答案

这里您使用的是自定义 Collection View 单元格,即 CollectionViewCell

因此,请尝试将其添加到您设计的自定义单元格 xib 或 Storyboard原型(prototype)单元格中

然后连接到 socket 上

然后根据条件尝试隐藏/取消隐藏该 ImageView

最初尝试将其隐藏在 xib 或 Storyboard中

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
}

关于ios - 滚动 swift3 时如何隐藏 UICollectionView 中第 0 行的锁定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47887192/

相关文章:

ios - Xcode 继续构建在 Swift 3 之上

swift - 在 Swift3 中基于 viewWithTag 更改 UIView

javascript - 在 iframe 中跳转到 anchor

ios - 谷歌地图 sdk 在 iPhone 中显示方向

ios - 如何从 objective-c 方法异步分派(dispatch) C 函数

ios - 通过类传递变量

ios - 使用音频队列 iOS api 将最后 30 秒的音频保存在文件中

ios - _ArrayType 在 Swift 3 中去了哪里?

javascript - 防止 div 被其下方的 div 碰撞时向上滚动?

html - 如何仅在需要时显示 Bootstrap 3 弹出窗口滚动