编辑时按钮文本不会保持更改 table

Button text not staying changed when editing table

我有一个 table 带有动态单元格的视图控制器。其中一个单元格有一个按钮来切换编辑table,这样用户就不太可能删除某些东西并且必须主动启用编辑。

当他们按下按钮时,应该切换编辑 table 和按钮的文本。编辑 table 有效,但按钮的文本只是闪烁,然后又恢复为原始文本。

@IBAction func deleteTouched(sender: UIButton) {
    editing = !editing

    let indexPath = NSIndexPath(forRow: 1, inSection: 0)
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! FieldInfoCell

    cell.deleteButton.titleLabel?.text = editing ? "Done" : "Delete Estimates"
}

我首先尝试在单元格中引用发件人而不是按钮

@IBAction func deleteTouched(sender: UIButton) {
    editing = !editing

    sender.titleLabel?.text = editing ? "Done" : "Delete Estimates"
}

为什么当我启用编辑时文本会闪烁到 Done 然后又回到 Delete Estimates

不要设置文本按钮的标题标签,而是使用 setTitle 方法,例如

button.setTitle("<#your title#>", forState: .Normal)

因为按钮需要每个状态的标题、标题颜色、图像、背景图像、属性标题等(.Normal.Selected.Highlighted 等)。每次按钮更改其状态时,这些属性都会根据状态进行刷新 - 即使您设置了 titleLabel.text 或 imageView?.image。您可以为 titleLabelimageView

设置字体、图层属性