【问题标题】:swift change label on uitableviewcell background color在uitableviewcell背景颜色上快速更改标签
【发布时间】:2016-08-25 07:35:09
【问题描述】:

我有带有自定义单元格的 UItablview 我需要在选择此行时更改标签背景颜色,但向下滚动时标签颜色会重复

【问题讨论】:

  • 能否请您展示一下您尝试过的内容?请花点时间阅读:How to Ask
  • 你是重复使用单元格,这是改变标签背景颜色的原因,你需要显示cellForRowAtIndexPath代码。
  • 该单元格在自定义单元格类中被选中,因此当我选择 + - 按钮时它具有颜色我如何使用 cellForRowAtIndexPath 来更新颜色

标签: ios swift uitableview uilabel


【解决方案1】:

您可以像这样子类化您的 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
    }
} }

【讨论】:

    【解决方案2】:

    我的理解是,你把代码放在tableview的didSelectRow方法中来改变颜色,但它在滚动时显示以前的颜色。

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 2010-09-18
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多