当我要用不同的状态填充tableView时,一个状态动作错误,我确定这是由于tableView的延迟引起的。请先观看视频和图片。
我像下面那样填充UITableView:
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId")
cell.state = myStates[indexPath.row]
}
在我的UITableViewCell
上:
var state: MyCustomDesignState? {
didSet {
self.button.set(state: self.state)
}
}
在我的UIButton
上:
func set(state: MyCustomDesignState?) {
switch state {
case ...
case .blackBack:
self.backgroundColor = background
self.setTitleColor(textColor, for: .normal)
self.frame = CGRect.init(x: 40, y: 190 - 40, width: 180, height: 34)
self.dropFlatShadow(color: #colorLiteral(red: 0.1008123383, green: 0.1008369699, blue: 0.1008091196, alpha: 1))
self.clipsToBounds = true
self.layer.cornerRadius = 5
}
}
这就是我想要的,以及我得到的东西:
tableView的外观如何:
要调试的视频:
答案 0 :(得分:0)
在您的TableViewCell
中:尝试以状态更改方式调用当前正在调用的此方法,如下所示:
override func layoutSubviews() {
super.layoutSubviews()
self.button.set(state: self.state)
}
希望我的问题正确,这对您有帮助。
答案 1 :(得分:0)
如果您已在Storyboard中使用前导约束和尾随约束设计了单元,则这些约束将通过自动布局“重新布局”。
因此,使用此行:
self.frame = CGRect.init(x: 40, y: 190 - 40, width: 180, height: 34)
仅在下一次自动布局通过之前会影响框架,例如在滚动tableView时。
您可能只想为按钮赋予Leading约束。如果您希望它比文本宽(也就是说,在左右两侧填充),请为按钮提供Content Inset
值。