ViewForHeaderInSection is called only once per UITableView

I've exhausted Google on this one, though I'm sure it's kind of awkward.

I am trying to create a UITableView representing 25 (let's say) objects in 25 sections of 1 row each.

The custom cells I create appear well (all 25 of them in the correct order). I would like the section title title to appear above the title of each object.

The problem is that only one kind of section title (the first one) is displayed.

Judicious NSLogging tells me that the viewForHeaderInSection is only called once, although the heightForHeaderInSection is called 2x for each object. I am returning 25 from numberOfSectionsInTableView and 1 from numberOfRowsInSection.

I am creating a UIView in viewForHeaderInSection by executing UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)]

and then adding UILabel and UIButton. I am not subclassing UITableViewHeaderFooterView. Not sure if it matters here.

Any ideas?

Relevant code:

Number of sections and lines:

- (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 presentation of 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;
}

      

+3


source to share


3 answers


It's too late though. after 30 minutes of fighting I found the solution in xCode 8.1. I am posting this which might help others.

// After adding the following delegate method , I found this several times



 - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section 
{
   return 30.0;
} 

      

+2


source


This doesn't answer the question, but the solution to this problem just came to my mind as I formatted above: if every cell needs a heading, just create the heading content in the cell, rather than trying to put it in a separate heading. Sigh...



It's still interesting to know why viewForHeaderInSection

it only seems to be called once.

0


source


add this delegate method

- (CGFloat) tableView: (UITableView*) tableView heightForFooterInSection: (NSInteger) section

      

-1


source







All Articles