Ios Swift:UICollectionView滚动时标签和图像的单元格复制
由于某些原因,当我滚动UICollectionView单元格上的标签时,会将标签添加到当前标签上,如下所示: 这是我的密码:Ios Swift:UICollectionView滚动时标签和图像的单元格复制,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,由于某些原因,当我滚动UICollectionView单元格上的标签时,会将标签添加到当前标签上,如下所示: 这是我的密码: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let myCell = collectionView.dequeueReusableCell(withReuseId
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
let centerY = (myCell.frame.size.height / 2) - 50
let title = PaddingLabel(frame: CGRect(x: 120, y: centerY, width: 200, height: 100))
let size = CGRect(x: 10, y: centerY, width: 100, height: 100)
let imageview: UIImageView = UIImageView(frame: size)
let image: UIImage = UIImage(named: categoryImages[indexPath.row])!
imageview.image = image
title.textAlignment = .left
title.clipsToBounds = true
title.numberOfLines = 2
title.font = title.font.withSize(25)
title.text = categoryTopics[indexPath.row]
switch (categoryColours[indexPath.row] ) {
case ("ef5c42"):
myCell.backgroundColor = UIColor(r: 239,g: 92,b: 66)
title.textColor = .white
case ("red"):
myCell.backgroundColor = UIColor.red
case ("green"):
myCell.backgroundColor = UIColor.green
case ("blue"):
myCell.backgroundColor = UIColor.blue
default:
myCell.backgroundColor = UIColor.yellow
title.textColor = .white
}
myCell.contentView.addSubview(title)
myCell.contentView.addSubview(imageview)
return myCell
}
我尝试补充:
for view in cell.subviews {
view.removeFromSuperview()
}
但这似乎只是显示了一个空单元格 您试图在
表格视图中创建自定义单元格,但在迭代单元格时添加视图。您需要为单元格创建自定义类,并向其中添加要使用的视图
然后您需要像这样将其出列:
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as! yourCustomClass
myCell.title.text = categoryTopics[indexPath.row]
然后只需将数据添加到yourCustomClass
中的视图中,如下所示:
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as! yourCustomClass
myCell.title.text = categoryTopics[indexPath.row]
及
您试图在表视图中创建自定义单元格,但在迭代单元格时添加视图。您需要为单元格创建自定义类,并向其中添加要使用的视图
然后您需要像这样将其出列:
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as! yourCustomClass
myCell.title.text = categoryTopics[indexPath.row]
然后只需将数据添加到yourCustomClass
中的视图中,如下所示:
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as! yourCustomClass
myCell.title.text = categoryTopics[indexPath.row]
及
如果你每次都要从头开始构建这个单元,那么你可以很懒,而不是让它出列
即改变:
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
致:
尽管我很惊讶,在退出队列后立即删除所有子视图是行不通的。我觉得下面的颜色很好
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
for view in myCell.contentView.subviews {
view.removeFromSuperview()
}
虽然我同意Barns52,但最好的做法是创建一个自定义UITableViewCell,使其与标签和图像等样式相匹配,然后每次设置图像和标签值,不要每次都重新创建它们并将它们添加为子视图。如果每次都要从头开始构建单元,那么您可以很懒,而不是将其出列
即改变:
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
致:
尽管我很惊讶,在退出队列后立即删除所有子视图是行不通的。我觉得下面的颜色很好
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
for view in myCell.contentView.subviews {
view.removeFromSuperview()
}
尽管我同意Barns52,但最好的做法是创建一个自定义UITableViewCell,使其与标签和图像等样式相匹配,然后每次设置图像和标签值,而不是每次重新创建它们并将其添加为子视图。我忘记在for循环中添加contentView,因此您得到了分数。这就解决了问题。谢谢。我忘了在for循环中添加contentView,您得到了分数。这就解决了问题。非常感谢。