【问题标题】:Swift TableViewCell custome didSelectSwift UITableViewCell 自定义 didSelect
【发布时间】:2020-10-01 19:17:42
【问题描述】:

我有一个客户TableViewCell。当它被点击时,它应该有一个cornerRadius + 一个带有.alpha(0.7) 的白色背景。现在它只是默认的grey,看起来像这样:

我尝试在我的setSelected() 中设置cornerRadiusbackgroundColor CustomCell,但这没有任何作用。

更新

我设法完成了,几乎:

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    self.selectedBackgroundView = UIView()
    self.selectedBackgroundView?.layer.cornerRadius = 10
    self.selectedBackgroundView!.backgroundColor = selected ? .clear : .white
    self.selectedBackgroundView?.alpha = 0.5
}

这很好用,但它唯一没有做的就是应用alpha-value。有什么想法吗?

【问题讨论】:

  • 你能贴一些代码吗?
  • @aheze 不太确定什么代码在这里有用。你需要什么?
  • setSelected 中的代码。另外,您要更改所选颜色还是突出显示颜色?按下时选中,按下时突出显示。
  • @gmdev 是的,我用didSelectRowAt 尝试过,但更改contentView.backgroundColor 不会做任何事情,仍然是默认的grey
  • 好的 - 如果您可以发布您尝试过的代码,就像@aheze 提到的那样,那就太好了。

标签: ios swift uitableview


【解决方案1】:

根据我的经验,您经常需要致电:

self.setNeedsLayout()

在 setSelected 函数中,以便实际更改单元格的视图。可能值得一试。否则,如果您可以发布您的 setSelected 函数,这将对您找到解决方案非常有帮助。

【讨论】:

    【解决方案2】:

    实际上是我自己发现的。在我的CustomCell里面:

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.selectedBackgroundView = UIView()
        self.selectedBackgroundView?.layer.cornerRadius = 10
        self.selectedBackgroundView!.backgroundColor = selected ? .clear : UIColor.white.withAlphaComponent(0.8)
    }
    

    感谢大家的帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      相关资源
      最近更新 更多