簡體   English   中英

uitableviewcell背景顏色上的快速更改標簽

[英]swift change label on uitableviewcell background color

我的UItablview具有自定義單元格,我需要在選擇此行時更改標簽背景顏色,但是向下滾動時會重復標簽顏色

在此處輸入圖片說明

您可以像這樣子類化Cell(然后cellForRow將不負責更新顏色,僅負責設置默認顏色)。

class YourTableViewCellClass: UITableViewCell {

    @IBOutlet weak var yourLabel: UILabel!

    override func setSelected(_ selected: Bool, animated: Bool) {
    if(selected) {

        self.contentView.backgroundColor = UIColor.red //or what you want as your cell bg color
        self.yourLabel.backgroundColor = UIColor.green //or what you want

    } else {

        self.contentView.backgroundColor = UIColor.white //or what you want as your cell bg color
        self.yourLabel.backgroundColor = UIColor.red //or what you want
    }
} }

我了解的是,您將代碼放在tableview的didSelectRow方法中以更改顏色,但是在滾動時它會顯示以前的顏色。

因此,您還需要在cellForRow方法中設置條件,例如

if(condition)
{
lbl.textcolor = x
}
else
{
lbl.textcolor = y
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM