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

时间:2016-08-16 15:44:25

标签: swift uitableview uitableviewsectionheader

我有一个UIViewController,它有一个UITableView和一个名为“A”的按钮。此表包含节和行。现在在我的场景中,我想强调点击“A”时的一个部分。我不想在viewForHeaderInSection中执行此工作,因为我不想重新加载整个表视图。我点击按钮时正在尝试的代码如下,

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)
    }

但不知何故,按钮点击时没有显示青色。任何人都可以在swift中发布一个合适的解决方案吗?

1 个答案:

答案 0 :(得分:0)

另一种方法是,如果按下按钮,则保持bool

然后,在你的:

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

here

方法,根据bool值更改背景颜色。选中该按钮后,只需翻转bool并调用:

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

ref

相关问题