bibscy bibscy - 3 months ago 16
Swift Question

call function before tableView:viewForHeaderInSection: is called - Swift

I have set up a tableView in a ViewController. This table view has 1 section, 2 prototype cells: one is used to return the number of rows in section, the other one is used to display a custom header.


  • In
    viewDidAppear()
    ,
    self.loadData()
    function is called to retrieve the data from NSUserDefaults. This data will be used in
    tableView:viewForHeaderInSection:
    to populate the labels in the header section.
    The problem is that
    tableView:viewForHeaderInSection:
    is automatically called before
    viewDidAppear()
    is called, as a consequence the labels in header section are not populated with the data retrieved from NSUserDefaults.
    How can I call
    self.loadData()
    before
    tableView:viewForHeaderInSection:
    is called?


Answer

This is a common problem — the data is not ready when the table view first appears. Simply give correct answers to the questions that the table view data source and delegate are asked, given the current state of things; if you have no data, say that there are no sections, for example. When you do have data, call reloadData on the table view to get the questions to be asked again.

Comments