3

I am trying to implement a header view in my application and have the following code to create the header view:

(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"FutureAppointmentsHeaderView" owner:self options:nil] lastObject];

    return view;
}

I have also set the height of the headerView as follows:

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

When the page loads, the header view is in the correct position (see first image). However when I scroll the header covers the other cells (see second picture.)

enter image description here

enter image description here

I can't what I am doing wrong to get this behaviour. Any help much appreciated.

pls
  • 1,522
  • 2
  • 21
  • 41

5 Answers5

3

This doesn't sound like a wrong behavior. This is how the header should act. Take, for example, the Contacts app on your iPhone - when you scroll, the header with the current letter always snaps to the top. When you continue scrolling, it is replaced by the next header in line.

What you should do is add a background color to the header instead of leaving it transparent. I think everything will look better and easier to understand once you do that.

Rony Rozen
  • 3,957
  • 4
  • 24
  • 46
  • 1
    Doh! In other apps I have added color and my brain didn't twig :-\. Thanks for the quick response (and reminder). – pls Aug 25 '15 at 14:25
2

Looks like you're header view is see through. Have you tried to put a background color ?

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"FutureAppointmentsHeaderView" owner:self options:nil] lastObject];
view.backgroundColor = [UIColor whiteColor];
wenders
  • 76
  • 3
1

try to change your tableViewType.

UITableViewStyleGroup can let your header scroll when you scroll the tableView

chaoxn
  • 103
  • 1
  • 8
1

You are adding a header for the section with - tableView:viewForHeaderInSection: . Section headers stay on top of the table view and will cover the cell(s) underneath it. If you would add another section to your table view there would be two identical header views for each section, because you are returning the view without checking the section index.

If you want a floating header use tableHeaderView. You can assign a view to UITableView's property in viewDidLoad or by dragging a view to the table view in interface builder. See Table Header Views in StoryBoards.

Community
  • 1
  • 1
marcusficner
  • 915
  • 8
  • 13
1

This is the default feature of tableview, and is not the bug. Just verify the background color of headerview in xib. It seems to have clear color, set it to any other color and you could find the difference. Code seems fine.

Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35