-2

I have a tableView with 2 sections, the sections array is fixed with 2 elements. The headers for the sections showed fine as code shows below in Xcode 7. I just upgraded to Xcode 8, and the section headers don't show anymore, the code below doesn't get called anymore.

Any ideas?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    NSString *date =[sections objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:date];
    [view addSubview:label];
    [view setBackgroundColor:[DeviceHelper Orange]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}
user1529412
  • 3,616
  • 7
  • 26
  • 42
  • 4
    That's not a table view delegate or data source method. – rmaddy Sep 22 '16 at 17:31
  • Does your [`tableView:viewForHeaderInSection:`](https://developer.apple.com/reference/uikit/uitableviewdelegate/1614901-tableview?language=objc) call this method? Or perhaps easier, just put this code in your `viewForHeaderInSection`. Bottom line, show us your `viewForHeaderInSection`. – Rob Sep 22 '16 at 17:37
  • I changed the above code to viewForHeaderInSection, still doesn't get called. BTW, the above code was getting called fine in xcode7, but not xcode8. – user1529412 Sep 22 '16 at 18:03
  • Similar query is answered on [this](http://stackoverflow.com/a/40764013/1673045) post. Hope this helps. – sudhanshu-shishodia Nov 23 '16 at 12:02

2 Answers2

7

Use this method as it is, you will see the header

NB : set the delegate and datasource of tableView

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *reuseId = @"reuseid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }

    cell.textLabel.text = @"Test";

    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    NSString *date = @"22nd Sept 2016";
    /* Section header is in 0th index... */
    [label setText:date];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor orangeColor]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/25

    return view;
}
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
  • Are you using Xcode 8 or xcode 7, because I have the same methods you do. Like I said it always worked, all I did was upgrade xcode, and headers just vanished. – user1529412 Sep 22 '16 at 18:32
  • XCode 8 beta version – Janmenjaya Sep 22 '16 at 18:33
  • 3
    I was actually missing - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; } ........ When I added this it worked fine...However, this wasn't needed in previous Xcode(7)... Thank you. – user1529412 Sep 22 '16 at 18:38
  • You are welcome. Happy to help. But i think this is also needed in XCode 7 as well. – Janmenjaya Sep 22 '16 at 18:39
2

Make sure you have heightForHeaderInSection in addition to your viewForHeaderInSection function

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