Reputation: 454
I've exhausted the Google on this one, though I'm sure it's something quite embarrassingly simple.
I am trying to create a UITableView representing 25 (let's say) objects in 25 sections of 1 row each.
The custom cells I am creating show up fine (all 25 of them, in the right order). I would like a section header view to be displayed above each object.
The problem is that only one section header view (the first) is being displayed.
Judicious NSLogging tells me that viewForHeaderInSection is called only once, although heightForHeaderInSection is called 2x per object. I am returning 25 from numberOfSectionsInTableView, and 1 from numberOfRowsInSection.
I am constructing a UIView in viewForHeaderInSection by doing UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)]
and then adding a UILabel and a UIButton. I am not subclassing UITableViewHeaderFooterView. Not sure if this makes a difference here.
Any ideas?
Relevant code:
Number of sections and rows:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.objects count];
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return 1;
}
Height and Cell for rows:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 390;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// add custom content to cell
return cell;
}
Height and view for Headers:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSLog(@"in viewForHeaderInSection %ld", (long)section); // I only see this once
NSLog(@"return view for section %ld", section); // this is 0 when I see it
// gather content for header
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)];
// add content to header (displays correctly)
return headerView;
}
Upvotes: 5
Views: 2288
Reputation: 2999
Though it too late. after 30 mins fighting I found a solution in xCode 8.1. I am putting it which may help to other.
// After adding the bellow delegate method, I found this called multiple times
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
Upvotes: 5
Reputation: 410
add this delegate method
- (CGFloat) tableView: (UITableView*) tableView heightForFooterInSection: (NSInteger) section
Upvotes: 0
Reputation: 454
This doesn't answer the question, but a solution to this problem just occurred to me as I was formatting the above: If each cell needs a header, just build the header content into the cell, rather than trying to put it in a separate header view. sigh...
Still interested to know why viewForHeaderInSection
seems to be called only once, though.
Upvotes: 0