【问题标题】:how to hide label?如何隐藏标签?
【发布时间】:2016-08-09 09:32:36
【问题描述】:

如您所见,我在这里有一个收藏视图列表,有些产品有促销价,有些没有。对于有促销的产品,它会显示红色的价格,并带有实际价格的划线(旁边)。现在的问题是,我正在使用 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] ))

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

    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
}

【问题讨论】:

  • 你能告诉我们一些你的代码吗?很难猜到...
  • @elyashiv 我更新了

标签: ios swift uilabel


【解决方案1】:

试试这个

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

}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 调试并找到正在执行的行。我的意思是检查哪个正在工作 if 语句或 else 语句。可能有一些错误
    【解决方案3】:

    出现这个问题是因为细胞重用

    试试这个代码:

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

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      相关资源
      最近更新 更多