Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide label?

Tags:

ios

uilabel

swift

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
}
like image 450
bobo Avatar asked Mar 26 '26 00:03

bobo


2 Answers

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

}

like image 182
Prashant Ghimire Avatar answered Mar 28 '26 03:03

Prashant Ghimire


You can hide label by changing alpha value also. Try

cell.priceLabel.alpha = 0 //to hide
cell.priceLabel.alpha = 1.0 //to show
like image 20
Chathuranga Silva Avatar answered Mar 28 '26 02:03

Chathuranga Silva