1

This may sound like a weird question as obviously there is always viewForHeaderInSection method available when working with tables in iOS.

However, to work with that function, one needs to reload the data of that table, something which I, in this case, want to avoid.

I have a button in my UITableViewSectionHeader, and I want to disable it on a certain point in my app - again - without having to reload the tableview data. I tried to do that with this piece code:

let currentTable = self.playerTables![i] as! UITableView
let header = currentTable.headerViewForSection(0)! as UITableViewHeaderFooterView!
let view = header.contentView as UIView
view.userInteractionEnabled = false

my app crashes when running this code, as the header is nil when loading the contentView. It does initialize when I call the currentTable.headerViewForSection(0)! code, but is nil again at the very next line.

I have checked whether the section i'm referring to is correct or not, checked apple docs and google/so and have not been able to find any reference on accessing the section header in the way I want. Is there a way?

Joris416
  • 4,751
  • 5
  • 32
  • 59
  • Assuming you don't have a large number of section headers, create them at viewDidLoad, then save in an array. When the system asks for one, you can pull it out of the array and not create it dynamically. Now they won't be re-used, and you can access the button whether the view is showing or not. I do this with tableViews vending settings all the time. [If this works for you I'll delete this comment and make it an answer.] – David H Jan 23 '16 at 15:57

1 Answers1

2

If the headerview is not on the screen (not visible), you cannot reach to it.

Maybe storyboard can help you. Drag an UIView into the tableview. This will be your headerview and you can connect controls directly to the UIViewController and access them.

Also you don't need to use viewForHeaderInSection for this example.

headerview in storyboard

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88