ios - 如何隐藏标签?

标签 ios swift uilabel

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

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

我尝试用这种方式隐藏标签,但它并没有真正起作用, 它工作了一段时间,在我开始滚动我的收藏 View 后,所有促销标签都被隐藏了

    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
}

最佳答案

试试这个

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

关于ios - 如何隐藏标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38847019/

相关文章:

iphone - 通过iPhone配置实用程序安装应用程序时出错

ios - 在 UISearchbar 和 UITextfields 下面添加一个下拉建议

ios - TabBarController 不会自动旋转

ios - 即使 if 语句不正确,Swift segue 也会运行

ios - 扫描二维码后如何进入新的 View Controller ?

iphone - 获取多行 UILabel 最后一行的宽度

ios - 隐藏带有 subview 引用的父 View

Swift 3 - 定义可能有两种类型的字典

xcode - 我可以用于 UILabel 的 swift 基本字体是什么

ios - 向 UILabel 添加左/右水平填充