1

I need this because I need to change the background color of a section. For example, if section at the top then blue green otherwise.

I have tried many things but I am with my ideas at the end.

Mirko Brunner
  • 2,204
  • 2
  • 21
  • 22

4 Answers4

2

Try this

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.backgroundColor = section == 0 ? [UIColor blueColor] : [UIColor greenColor];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    // header.contentView.backgroundColor = [UIColor blackColor];
}

Considering top means 1st section.

Edit

If you want to change color according to scrolling and identifying which section is on top and which is not then you have to implement UIScrollViewDelegate so that you can handle scrolling delegates. You can try something like this

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSIndexPath *firstVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:0];
    NSLog(@"first visible cell's section: %i, row: %i", firstVisibleIndexPath.section, firstVisibleIndexPath.row);
}

References: UIScrollViewDelegate implementation and Detecting top cell in TableView

Community
  • 1
  • 1
Faisal Ali
  • 1,135
  • 10
  • 17
  • Not really, i mean a section is on top. I know my question is not clear enough. But i will try you solution. It sounds very good. One think: 'view.backgroundColor' is deprecated use 'contentView.backgroundColor' say´s Xcode. – Mirko Brunner Apr 04 '14 at 18:01
  • This is incorrect. Section 0 may or may not be displayed depending on content length and number of sections. – Léo Natan Apr 04 '14 at 18:01
  • Your solution is clean :) I only gave the path to follow but you answered it precisely (Y) – Faisal Ali Apr 04 '14 at 18:20
  • It´s not really correct working but i will try fix it. – Mirko Brunner Apr 04 '14 at 18:28
  • That is the spirit my friend :) – Faisal Ali Apr 04 '14 at 18:30
  • After your edit, I removed my downvote. The problem with `scrollViewDidEndDragging` is it will only work after a user lifts his finger. – Léo Natan Apr 04 '14 at 18:30
  • - (void)scrollViewDidScroll:(UIScrollView *)scrollView can help in that case, I hope so. – Faisal Ali Apr 04 '14 at 18:37
  • 1
    That would be too slow, as after every scroll point, you will query the table view for displayed cells' index paths. `willDisplayHeaderView` is the correct spot. – Léo Natan Apr 04 '14 at 19:13
1

Try this:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    NSArray* visibleCellIndexPaths = [tableView indexPathsForVisibleRows];

    //Set all header views to have a blue background color.
    [view setBackgroundColor:[UIColor blueColor]];

    if(visibleCellIndexPaths.count > 0)
    {
        //Set the topmost visible section header view to have green background color.
        [[tableView headerViewForSection:[visibleCellIndexPaths[0] section]] setBackgroundColor:[UIColor greenColor]];
    }
}
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
0

Just check if the frame of the section header you're looking for has moved to the top.

smoothBlue
  • 203
  • 4
  • 12
0

Swift version of @faisal Ali answer

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    view.backgroundColor = section == 0 ? UIColor.white : UIColor.clear
}
user2201
  • 29
  • 7