Search code examples
iosswiftuitableviewstackview

Stackview is hidden in reusable tableview cell after inserting cells


I have a tableview for showing some products, the products may or not have a discount, the discounts (max 2) are grouped in a stackView, so in code I hide or show the stack view if the product have a discount.

The problem comes when I insert a new cell, suddenly the cell that holds a product with discount doesn't have the stack view visible.

I tried with the 2 methods for dequeue cells, when I use,

tableView.dequeueReusableCell(withIdentifier:, forIndexPath)

the problem occurs when Inserting but when I use,

tableView.dequeueReusableCell(withIdentifier:)

the problem at inserting disappear, but occurs again when I scroll down to make the cell not visible and scroll back.

This is the code in cell for row:

let basicCell = tableView.dequeueReusableCell(withIdentifier: "basicCell") as! BasicCell
        if product.discounts{
            basicCell.discountType = DiscountType.lineDiscount
        }else{
            basicCell.discountType = DiscountType.none
        }
        basicCell.configureCellType()

        return basicCell

And the code of configureCellType():

func configureCellType(){
    switch discountType! {
    case .none:
        discountStackView.isHidden = true
    case .lineDiscount:
        groupDiscountView.isHidden = true
    case .groupDiscount:
        lineDiscountView.isHidden = true
    case .bothDiscounts: break
    }
}

Solution

  • well the problem is actually in your configureCellType() function. As each case is hiding a stack view .. Check it out