2016-08-09 180 views
2

正如你所看到的,我在這裏有一個收集視圖列表,有些產品有促銷價格,有些則不是。對於那些有促銷的產品,它會顯示紅色的價格與實際的價格罷工通過它(旁邊)。現在的問題是,我是通過使用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 
} 
+0

你能告訴我們你的一些代碼嗎?這很難猜測它... – elyashiv

+0

@elyashiv我更新 – bobo

回答

2

試試這個

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 

}

0

您也可以通過更改alpha值來隱藏標籤。嘗試

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

不工作,同樣的問題): – bobo

+0

調試並找到執行哪條線。我的意思是檢查哪一個正在工作,如果陳述或其他聲明。這可能是一些錯誤 –

0

的發生是因爲細胞重用

試試這個代碼的這個問題:

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