I have a custom cell for my collectionview and one of the labels in the cell disappears when I scroll down and back up to the cell again.
Here is my 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
}
} else
if collectionView == categoryCollectionView{
//...this is another collection and not the collectionView that I have problem with
return cell
}
return UICollectionViewCell()
}
}
And here is what I have in y custom cell for the label that is disappearing when I scroll down and back up to the cell:
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
}
}
}
I have the label pricePerAmountLbl.isHidden
set to true and false when needed but still I dont understand why its disappearing. When I dont set the hidden status for the label in the custom cell the content of the label gets repeated in all cell which is not right either.
Edit:
I added the following to the cell but still no success the problem is still there
else if product.pricePerAmountExist == false {
pricePerAmountLbl.isHidden = true
} else {
pricePerAmountLbl.isHidden = false
}
There was nothing wrong with my code! The error was happening in storyboard. My label was stored in a stackview and that stackview was stored in another stackview; when I was scrolling and the label would go hidden the stackview height would have gone to zero and it stayed at zero even though I unhide the label.