0

I have a UITableView of static cells, created using storyboards with two sections. I use the following code to change the look of section headers.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{    
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }


    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(20, 1, 320, 20);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor lightGrayColor];
    label.shadowColor = [UIColor grayColor];
    label.shadowOffset = CGSizeMake(-1.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:10];
    label.text = sectionTitle;

    UIView *view = [[UIView alloc] init];
    [view addSubview:label];

    return view;
}

It looks the way I want, but when scrolling tableView, the label in the first Header doesn't scroll with the UIView. It stays on the middle of the screen. As for the label in the second header, it behaves like it is supposed to.

Jorge
  • 1,492
  • 1
  • 18
  • 33
  • I think you can just return UILabel instead of wrapping it in UIView and As per https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:viewForHeaderInSection: You must also implement tableView:heightForHeaderInSection: – Piyuesh Jan 10 '14 at 15:54
  • It does work returning the `UILabel`, but I get the same problem. Thanks. – Jorge Jan 10 '14 at 15:58
  • iOS has sticky sections headers. So when you scroll they stay visible at the top of the table view, and only scroll off when the user scrolls past the end of their section. If you go to any of Apple's apps, Music for example, you will see this behaviour in the list of songs, with the letters in section headers. This helps to keep context for the user. This question may be of use to you: http://stackoverflow.com/questions/664781/change-default-scrolling-behavior-of-uitableview-section-header – George Green Jan 10 '14 at 16:54
  • Kind of what is happening. Is there a way around this? Also, the `label` is sticking, but the `UIView`is not, what is making the problem worse since content pass under the `label`. Thanks. – Jorge Jan 10 '14 at 17:03

2 Answers2

0

In case you have not also implemented tableView:heightForHeaderInSection:, according to Apple docs it is required:

The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.

That could be causing part of your problem.

Paul Bonneville
  • 1,905
  • 2
  • 13
  • 17
  • I just added this method, but the problem persists. Thanks. – Jorge Jan 10 '14 at 22:34
  • Can I assume that you are working in a UITableViewController? Just to test it out, I created a new UITableViewController, made it static, commented out the table data source delegate methods and added you exact method and it worked without the issue you are having. – Paul Bonneville Jan 10 '14 at 23:37
0

I just encountered this problem and discovered that my issue was having a header view with the background color set to clearColor. Using anything else and the headers were sticking properly.

Zorm
  • 46
  • 3