【问题标题】:Button text not staying changed when editing table编辑表格时按钮文本不会保持更改
【发布时间】:2016-01-11 15:38:26
【问题描述】:

我有一个带有动态单元格的 tableview 控制器。其中一个单元格有一个用于切换编辑表格的按钮,这样用户就不太可能删除某些内容并且必须主动启用编辑。

当他们按下按钮时,它应该切换编辑表格和按钮的文本。编辑表格有效,但按钮的文本只是闪烁然后返回原始文本。

@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

【问题讨论】:

  • 而不是 Button.titleLabel?.text 尝试 Button.titleLabel?.setTitle(title:forState...
  • @Johnykutty 就是这样,谢谢!

标签: swift uitableview editing


【解决方案1】:

使用 setTitle 方法代替设置文本按钮的标题标签

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多