2016-08-25 120 views
1

我UItablview与自定义单元格,我需要的时候选择该行来更改标签的背景颜色,但重复标签的颜色,当向下滚动迅速改变标签

enter image description here

+0

请问您可以使用您试过的东西吗?请花一些时间阅读:[问] –

+0

您正在重复使用单元格,这是更改标签背景颜色的原因,您需要显示cellForRowAtIndexPath代码。 –

+0

在自定义单元格类中选中单元格,以便在选择+ - 按钮时它具有颜色我如何使用cellForRowAtIndexPath更新颜色 –

回答

2

你可以继承你的单元格就像这样(然后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 
    } 
} } 
1

我明白了什么是,你把代码中的tableview改变颜色的didSelectRow方法,但它表明先前的颜色,而滚动。

因此,您需要在cellForRow方法中设置条件,例如

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