我有一个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
}
答案 0 :(得分: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
}
答案 1 :(得分:0)
您正在使用此方法在imageView1
中添加UIImageView cell.imageView
cell.imageView.addSubview(imageView1)
如果要将其直接添加到单元格中,则需要从其超级视图中删除,因为imageView1
没有前一个单元格的引用,我们使用单元格重用。
对于您可以使用上述解决方案,或者您需要为自定义单元格添加锁定图像并保持隐藏/显示:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
答案 2 :(得分: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
}
答案 3 :(得分: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
中添加视图。