![](/img/trans.png)
[英]How to change UITableviewCell background color on swipe action in Swift?
[英]swift change label on uitableviewcell background color
您可以像這樣子類化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.