【问题标题】:Label background colour changed when UITableViewCell selected选择 UITableViewCell 时标签背景颜色发生变化
【发布时间】:2018-04-04 21:01:00
【问题描述】:

我在 UITableViewCell 中添加了一个自定义标签。它的背景颜色为红色,字体为白色。当我选择任何单元格时,所选单元格的标签背景颜色变为灰色。

供参考,我正在添加代码

let lblUnreadCount = cell?.viewWithTag(3) as! UILabel
let size:CGFloat = 20.0
lblUnreadCount.bounds = CGRect(x: 0.0, y: 0.0, width: 28.0, height: 20.0)
lblUnreadCount.layer.cornerRadius = size / 2
lblUnreadCount.layer.borderWidth = 1.0
lblUnreadCount.layer.backgroundColor = UIColor.red.cgColor
lblUnreadCount.layer.borderColor = UIColor.red.cgColor

作为参考,我还添加了一个屏幕截图。

【问题讨论】:

  • 将选择设置为无

标签: ios iphone uitableview swift3 uilabel


【解决方案1】:

似乎将单元格样式设置为 .none 不是 OP 想要的,因为它实际上会在选中时清除单元格的突出显示颜色。

我发现这样实现有点棘手(清除 TextLabel bg 颜色并将 bg 颜色设置为其图层):

// Note that cell background color must be cleared **before** setting layer color
lblUnreadCount.backgroundColor = UIColor.clear
lblUnreadCount.layer.backgroundColor = UIColor.red.cgColor

这样,当单元格被选中并突出显示时,您将能够保持文本标签(在单元格内)的背景颜色不变。

【讨论】:

    【解决方案2】:

    在 UITableViewCell 中设置标签的背景颜色。在上面的代码中,您可以设置标签的图层背景颜色。

    lblUnreadCount.backgroundColor = UIColor.red.cgColor

    【讨论】:

      【解决方案3】:

      1.设置选择样式无

      UITableViewCell | |----- UIView | |-----UIImageView-> eg:Message icon |-----Title Label-> eg:Message |-----Count Label-> red background

      2.选择单元格时,您必须更改UIView背景颜色(灰色)。

      【讨论】:

        【解决方案4】:

        改变的不是标签的背景,而是单元格本身。在你的cellForRow 方法中,在你dequeued 单元格之后,使用这个

        cell.selectionStyle = .none
        

        【讨论】:

        • 但这并没有显示突出显示的选定单元格。我也想要。
        • 你想让它在选中时变成红色吗?
        • 选中时更改视图背景颜色。
        猜你喜欢
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 2014-02-12
        • 2015-06-11
        • 1970-01-01
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        相关资源
        最近更新 更多