Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewForHeaderInSection is called only once in UITableView

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;
}
like image 591
wallace Avatar asked Aug 29 '14 07:08

wallace


1 Answers

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;
} 
like image 187
Jamil Avatar answered Sep 22 '22 12:09

Jamil