1
votes

I'm working on implementing UITableView section headers similar to the Photos app on iPhone. The top header view should have a grey background and the other header views should have a white background. Based on that I have a couple of questions I need help with:

  1. How do I find out which section header is on top (e.g. adjacent to the nav bar)? Note, that the navigation bar is translucent (meaning it displays cells below itself), so I can't go by finding visible cells and then retrieving section based on that.

  2. What should be the approach to change background color of the top section header to a different color and then back to the original color when it's no longer a top one?

1
Are you asking about section header? - Himanshu Joshi
@Morpheus - yes, sections headers. - user3600656

1 Answers

0
votes

Make the property NSUInteger sectionPath and initialize it with 0 in viewDidLoad.

Create a headerView and for specific section change backgroundColor of that view.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];

    headerLabel.textAlignment = NSTextAlignmentCenter;
    headerLabel.text = [titleArray objectAtIndex:section];

    if(section == sectionPath) {
        headerLabel.backgroundColor = [UIColor grayColor];
    }
    else {
        headerLabel.backgroundColor = [UIColor whiteColor];
    }

    [headerView addSubview:headerLabel];    

    return headerView;
}

To dynamically find the top-most header in UITableView during scrolling

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    for (NSIndexPath* i in [yourTableViewName indexPathsForVisibleRows]) {
      sectionPath = [i indexAtPosition:0];
    }
}