我有一个用于collectionview的自定义单元格,当我向下滚动并再次回到该单元格时,该单元格中的标签之一消失。
这是我的cellForItemAt
:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == productCollectionView {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ProductCell, for: indexPath) as? ProductCell {
if cartProducts.contains(products[indexPath.item]) {
//this is the product
var product = products[indexPath.item]
//get the index from cartProducts
let index = cartProducts.firstIndex(of: product)
//Get the cartcount and update the product
let cartCount: Int = cartProducts[index!].cartCount
product.cartCount = cartCount
products[indexPath.item] = product
//now set the updated product for cell.product
cell.product = products[indexPath.item]
cell.delegate = self
return cell
} else {
cell.product = products[indexPath.item]
cell.delegate = self
return cell
}
}
return UICollectionViewCell()
}
}
这是我在y自定义单元格中所拥有的标签,当我向下滚动并返回到该单元格时,该标签消失了:
class ProductCell: UICollectionViewCell {
var product: Product!
{
didSet {
commonUIUpdate()
}
}
func commonUIUpdate() {
//set the price per amount and its unit
if product.pricePerAmountExist == true {
pricePerAmountLbl.isHidden = false
if let PricePerAmount = formatter.string(from: Double(product.pricePerAmount)/100 as NSNumber) {
let PricePerAmountVoume = Float(String(Double(product.pricePerAmountVolume)/100))?.clean ?? ""
pricePerAmountLbl.text = "\(PricePerAmount)/\(String(describing: PricePerAmountVoume))\(product.pricePerAmountUnit)"
}
} else
if product.pricePerAmountExist == false {
pricePerAmountLbl.isHidden = true
}
}
}
我在需要时将标签pricePerAmountLbl.isHidden
设置为true和false,但我仍然不明白为什么它消失了。当我不在自定义单元格中设置标签的隐藏状态时,标签的内容将在所有单元格中重复出现,这也不对。
您未处理所有案件。您的标签保持隐藏状态,因为它在上一个单元格中被设置为隐藏。您可能需要更改代码以覆盖丢失的情况。希望对您有所帮助:
else if product.pricePerAmountExist == false {
pricePerAmountLbl.isHidden = true
} else {
pricePerAmountLbl.isHidden = false
pricePerAmountLbl.text = "This case was not handled before"
}