You should implement heightForHeaderInSection:
and set the height for the header to a value > 0.
viewForHeaderInSection: not called when reloadData: is called
-
21-07-2023 - |
Pregunta
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;
}
Solución
Otros consejos
Same issue occured with me that's because tableview didn't find height for header but as I was using automatic height calculation from xCode 9, I cannot give any explicit height value. After some experimentation I got solution, we have to override this method as,
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForHeaderInSection:(NSInteger)section
{
return 44.0f;
}
Although I have checked both options
- Automatic Height Calculation
- Automatic Estimated Height Calculation
from storyboard as apple says, but still I got this weird error.
Please Note: This Error was shown only on IOS-10 version not on IOS-11 version. Maybe it's a bug from xCode. Thanks