我有一个cell
看起来像这样的结构:
的与(蓝色)contentView
相同backgroundColor
,tableView
background
而嵌套的则为view
白色,四周有cornerRadius
5p的autoLayout
空白,导致看起来像是分离的cells
,有点像是buttons
连续分离的(让我知道是否需要图像)。
每当我单击a时,cell
我都希望cell
将其突出显示为另一种颜色。通常,此操作将storyBoard
在下进行attribute inspector
selection
。但这严格连接到,cell
并且不会影响嵌套views
。我可以将其views
透明化或完全去除它们,然后可以使用,但是后来我错过了split的错觉cells
。
我在这里阅读了如何在UITableViewCell之间添加可用于sections
分隔的间距cells
。很好,但是事情是我正在使用RxSwift
,如果我想要numberOfSections
delegate
,那么我也需要cellForRowAt
和numberOfRows
与我的冲突Rx
tableView
。
我尝试通过编程将其添加tag
到我的nested中view
,以这种方式获取它:
var myView = self.contentView.viewWithTag(1) //self is cell
然后在color
中设置Rx
subscription
:
cell?.layer.backgroundColor = UIColor.green.cgColor
但这是行不通的。任何想法如何解决这个问题?
覆盖func setSelected(_ selected: Bool, animated: Bool)
单元格中的方法,并根据selected
属性调整视图。
另外,rx.methodInvoked
如果您不想覆盖此方法,则可以这样做。
根据要求,这是使用的方法rx.methodInvoked
。
_ = rx.setSelected
.map { $0.selected ? UIColor.gray : UIColor.white }
.takeUntil(rx.deallocating)
.bind(to: contentView.viewWithTag(1)!.rx.backgroundColor)
要使上述工作正常进行,请在下面的某处添加代码:
extension Reactive where Base: UITableViewCell {
var setSelected: Observable<(selected: Bool, animated: Bool)> {
base.rx.methodInvoked(#selector(UITableViewCell.setSelected(_:animated:)))
.map { (selected: $0[0] as! Bool, animated: $0[1] as! Bool) }
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句