更改tableview部分标题中标签的文本

时间:2017-10-27 10:05:07

标签: ios swift uitableview

我有一个带有自定义标题的可扩展tableview。当表扩展时,我需要在标题中更改标签的文本。我使用下面的代码。

func toggleSection(header: SuperHeaderDelegate, section: Int) {
        sections[section].expanded = !sections[section].expanded

        tableView.beginUpdates()
        tableView.reloadSections([section], with: .automatic)
        tableView.endUpdates()

        if let head = header as? SavingAccountHeaderView {
            head.accNoLabel.text = "HIIIIIII"
        }
        else { print("NOPE") }

    }

当我使用此代码时,它会将 accNoLabel 文本更改为新文本,然后再将其更改回旧文本。

我尝试了tableView.reloadData()而不是tableView.reloadSections(),然后代码运行正常。 accNoLabel 未更改回旧文本。但我真的需要使用tableView.reloadSections()来使用动画。 那么有谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:1)

你应该检查几件事:

  1. 你的'扩展'切换是否发生在正确的地方(和时间)?
  2. reloadSections()是一个异步函数。因此,您不能指望在更新完成后“更新”文本。
  3. 我建议在viewForHeader函数中切换标签的文本,因为此时你可以确定正在调用reloadSections()方法并且部分的状态已经更新。

相关问题