如何隐藏标签?

how to hide label?

如您所见,我这里有一个 collection 查看列表,有些产品有促销价,有些则没有。对于有促销的产品,它会显示红色价格和实际价格(旁边)。现在的问题是,我使用 segue 传递了之前视图中的所有这些值,现在我必须为那些没有促销价的产品隐藏促销价标签,我应该怎么做?

hide label

代码如下:

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] ))

我尝试用这种方式隐藏标签,但它并不奏效, 它工作了一段时间,在我开始滚动我的 collection 视图后,所有促销标签都被隐藏了

    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
}

您也可以通过更改 alpha 值来隐藏标签。尝试

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

This problem occurs because of cell reusing

试试这个代码:

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

试试这个

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

}