0

I have a UIViewController which has a UITableView and a button called "A" . This table has sections and rows. Now in my scenario i want to highlight a section when "A" clicked. I don't want to do this work in viewForHeaderInSection as i don't want to reload the whole table view. The code that i am trying at the moment on button click is as follows,

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

but somehow it is not showing the cyan color on button click. Can anyone please post a proper solution in swift?

neena
  • 365
  • 1
  • 6
  • 22

1 Answers1

0

An alternate approach would be to keep a bool if the button has been pressed or not.

Then, in your:

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

ref

method, change the background color based on that bool value. When the button is selected, simple flip the bool and call:

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

ref

random
  • 8,568
  • 12
  • 50
  • 85