Search code examples
objective-cios7uitableview

viewForHeaderInSection: not called when reloadData: is called


I made a UITableView and set the "delegate" and "datasource" and every time I call reloadData, it goes into the method:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.headersList count];
}

And the method:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SectionInfo *headerInfo = (self.headerInfoArray)[section];
NSInteger numOfObjectsInSection = [[headerInfo.list objectsInList] count];
    return headerInfo.open ? numOfObjectsInSection : 0;
}

And then stops! it doesn't go into the ViewForHeaderInSection: method. I have also implemented the method:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_HEADER_HEIGHT;
}
  • Knowing that I use the open/close section feature! so at first all the sections are closed and the number of rows in each one is 0 but the number of sections returned is correct (when a section is opened the number of rows is updated).
  • The only way for it to show the header views is to wait for some time until it's automatically reloaded! or I swipe up or down!

The viewForHeaderInSection method:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UISectionHeaderView *sectionHeaderView = [[UISectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, SECTION_HEADER_HEIGHT)];

    SectionInfo *sectionInfo = (self.headerInfoArray)[section];
    sectionHeaderView.open = sectionInfo.open;
    sectionInfo.headerView = sectionHeaderView;

    sectionHeaderView.titleLabel.text = [NSString stringWithFormat:@"%@ (%lu)",sectionInfo.list.title, (unsigned long)[sectionInfo.list.objectsInList count]];
    sectionHeaderView.section = section;
    sectionHeaderView.delegate = self;

    return sectionHeaderView;
}

Solution

  • You should implement heightForHeaderInSection: and set the height for the header to a value > 0.