I'm setting a header for a UITableView
's sections and everything works perfectly :
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let headerView = MyCustomHeaderView()
headerView.backgroundColor = UIColor.blueColor()
return headerView
}
The header of every section is blue, as I expect it to be.
Now, I'd like to change this color when my UITableView
is scrolling so I implemented this method in a UITableView
extension :
extension UITableView
{
func setHeadersColor(color: UIColor)
{
guard let delegate = self.delegate
else {return}
guard delegate.respondsToSelector(#selector(UITableViewDelegate.tableView(_:viewForHeaderInSection:)))
else {return}
for i in 0...self.numberOfSections
{
if let header = delegate.tableView!(self, viewForHeaderInSection: i) as? MyCustomHeaderView
{
header.backgroundColor = color
}
}
}
}
When I call this method, header.backgroundColor = color
gets called but the background color of the header doesn't change.
So my questions are :
is the instance of
UIView
returned bytableView:viewForHeaderInSection:
the same as the actualUIView
displayed on the header ? Is it a copy ? Is it something else ?how could I change this header view dynamically ? Do I have to call
reloadData
just to change my header's background color ? That would be a waste of resources..