bobo
bobo

Reputation: 121

how to hide label?

As you can see, i have a list of collection view here, and some product are having promotion price and some are not. For those product which having promotion, it will display the red colour price with the actual price strike through with it(beside). The problem now is, i was passing all these value from previous view using segue, now i have to hide promotion price label for those product which not having promotion price, how should i do it?

hide label

Here is the code:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! SubCategoryDetailsCollectionViewCell

    let grey = UIColor(red: 85.0/255.0, green: 85.0/255.0, blue: 85.0/255.0, alpha: 1.0)
    cell.layer.borderWidth = 1.0
    cell.layer.borderColor = grey.CGColor

    cell.titleLabel.text = name[indexPath.row]
    cell.imageView.sd_setImageWithURL(NSURL(string: thumbImg1[indexPath.row] ))

I try to hide the label in this way, but its not really working, it work for awhile and after i start scrolling my collection view, all promo label is hidden

    if promo[indexPath.row] == "0"{

        cell.promoLabel.hidden = true
    }else{
        cell.promoLabel.text = "RM" + promo[indexPath.row]
    }

    cell.priceLabel.text = "RM" + price[indexPath.row]

    cell.productLabel.text = label[indexPath.row]

    cell.setNeedsDisplay()
    return cell
}

Upvotes: 3

Views: 3487

Answers (3)

Prashant Ghimire
Prashant Ghimire

Reputation: 548

try this

if promo[indexPath.row] == "0"{
    cell.promoLabel.hidden = true
}else{
   cell.promoLabel.hidden = false
    cell.promoLabel.text = "RM" + promo[indexPath.row]
}


cell.productLabel.text = label[indexPath.row]

cell.setNeedsDisplay()
return cell

}

Upvotes: 4

Kumar
Kumar

Reputation: 1942

This problem occurs because of cell reusing

Try this code:

if promo[indexPath.row] == "0" {
   cell.promoLabel.hidden = true
}
else {
   cell.promoLabel.hidden = false
   cell.promoLabel.text = "RM" + promo[indexPath.row]
}

Upvotes: 0

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

You can hide label by changing alpha value also. Try

cell.priceLabel.alpha = 0 //to hide
cell.priceLabel.alpha = 1.0 //to show

Upvotes: 1

Related Questions