【问题标题】:Assigning to property of type `UIView` has no effect分配给“UIView”类型的属性无效
【发布时间】:2018-09-06 18:44:05
【问题描述】:

我使用的是 Xcode 10.0 beta 6,所以这可能只是一个错误。


我遇到了一个问题,将新的UIView-instance 分配给属性似乎对该属性没有任何影响。

所述属性是leadingView in ViewTextFieldCell:

class ViewTextFieldCell: UITableViewCell {

   // ...

   var leadingView = UIView()

   // ...
}

当通过以下方式修改单元格实例时...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = ViewTextFieldCell(style: .default, reuseIdentifier: "TestCell")

   cell.leadingView.backgroundColor = .blue

   let otherView = UIView()
   otherView.backgroundColor = .green
   cell.leadingView = otherView

   return cell
}

...唯一保留的更改是将背景颜色设置为蓝色。
otherView的赋值好像没有效果。

这怎么可能?

【问题讨论】:

    标签: swift uitableview uiview


    【解决方案1】:

    从您的示例中不清楚“leadingView”会发生什么其他初始化,但是替换“otherView”没有作为子视图添加到单元格中,也没有框架或约束。

    【讨论】:

    • 我假设与视图关联的自动布局约束绑定到属性,而不是 UIView-instance;这当然是错误的。为leadingView 更新didSet 中的布局解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2021-06-22
    • 2020-05-28
    • 1970-01-01
    相关资源
    最近更新 更多