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