您将单元格出列,并且每次添加子视图时都不会检查它们是否已经存在,以防再生单元格出现。这可能会破坏约束并导致不正确的大小调整。
舍入的问题相同 - 您设置了圆角,但是当重用的单元格不应该舍入时,您永远不会还原此行为。
解决此问题的最佳方法是仅添加一次检查并创建子视图:
func setup(answer: String, isSelected: Bool, style: Style, isLastInSection: Bool) { if self.subviews.count == 0 { // adding subviews etc. // code that needs to be performed only once for whole cell's life } self.isLastInSection = isLastInSection // set up content that changes for each cell (like text) // for example a code depending on parameters of this method }
或者你可以保留一些属性 isInitialized 并在开始时检查。
isInitialized
也是你的方法 layoutSubviews 必须支持两种情况:
layoutSubviews
override func layoutSubviews() { super.layoutSubviews() if isLastInSection { roundCorners(corners: [.bottomLeft, .bottomRight], radius: 16.0) } else { layer.mask = nil } contentView.layoutIfNeeded() }