【问题标题】:UITableViewCell highlight overlays my custom viewsUITableViewCell 高亮覆盖了我的自定义视图
【发布时间】:2019-05-08 10:35:11
【问题描述】:

我的应用中有一个自定义 UITableViewCell。我在初始化时向单元格添加了一个视图。

class RestaurantCell: ListTableViewCell {
    public let stripeView: UIView = UIView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        addSubview(stripeView)
    }
}

现在,当用户触摸应用程序时,灰色突出显示将覆盖我的自定义视图,但不会覆盖 textLabeldetailTextLabel。 为了解决这个问题,我尝试在textLabelselectedBackgroundView 上方添加stripeView。那并没有解决它。有什么想法吗?

【问题讨论】:

    标签: ios swift uitableview cell


    【解决方案1】:

    您要添加的元素应该是“自定义视图”的子元素才能突出显示,您能检查 textLabel 和 detailTextLabel 是否都是子元素吗?

    【讨论】:

    • 所以我现在将stripeView 添加到contentView,因为这也是iOS 添加默认textLabeldetailTextLabel 的地方。但它仍然消失了。
    【解决方案2】:

    事实证明,视图并没有消失。 UITableViewCell 只是设置单元格中每个UIViewbackgroundColor 属性。

    添加下面的代码,成功了。

    override func setSelected(_ selected: Bool, animated: Bool) {
        let color = stripeView.backgroundColor
        super.setSelected(selected, animated: animated)
    
        if selected {
            stripeView.backgroundColor = color
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        let color = stripeView.backgroundColor
        super.setHighlighted(highlighted, animated: animated)
    
        if highlighted {
            stripeView.backgroundColor = color
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-12
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多