更新部分背景颜色而不刷新整个 UITableView

update section background colour without refreshing the whole UITableView

我有一个 UIViewController,它有一个 UITableView 和一个名为 "A" 的按钮。此 table 包含部分和行。现在在我的场景中,我想在 "A" 单击时突出显示一个部分。我不想在 viewForHeaderInSection 中做这项工作,因为我不想重新加载整个 table 视图。我现在尝试点击按钮的代码如下,

public func scrollToSection(sectionNumber:Int)
    {
      let indexPath = NSIndexPath(forRow: 0, inSection: sectionNumber)
      let headerView = tableView.headerViewForSection(indexPath.section)
      tableView.beginUpdates()
      headerView?.contentView.backgroundColor = UIColor.cyanColor()
      tableView.endUpdates()
      tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
    }

但不知何故,点击按钮时它没有显示青色。谁能 post 在 swift 中找到合适的解决方案?

另一种方法是在按钮是否被按下时保留 bool

然后,在你的:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

ref

方法,根据 bool 值更改背景颜色。选择按钮后,简单地翻转 bool 并调用:

func reloadSections(_ sections: NSIndexSet, withRowAnimation animation: UITableViewRowAnimation)

ref