views:

2038

answers:

7

I have a UITableView with two sections. It is a simple table view. I am using viewForHeaderInSection to create custom views for these headers. So far, so good.

The default scrolling behavior is that when a section is encountered, the section header stays anchored below the Nav bar, until the next section scrolls into view.

My question is this: can I change the default behavior so that the section headers do NOT stay anchored at the top, but rather, scroll under the nav bar with the rest of the section rows?

Am I missing something obvious?

Thanks.

A: 

I am actually experiencing the opposite problem, and searching for the way to enable these headers to sit above the scrolling cells. I cannot say for certain that this is the default behavior, but I certainly haven't told it to do anything special. I'm actually setting the section header view in my table controller's init function, like this:

    [self.view setTableHeaderView:navShadow];

I have no idea if this is the best way. (In fact, I'm pretty sure it's not, but it puts the navShadow view at the top of the table, which is ...almost... what I want.)

livingtech
You're talking about a table header (that is, a view that sits at the top of a table). The post is talking about a section header (that is, a view that contains the title of each section). The default (only?) behavior of the table header is to scroll with the table. The default (only?) behavior of a section header is to stay anchored at the top of the table's scroll view until the next header comes along.
jemmons
Ahhh, thanks jemmons! You're absolutely right.
livingtech
A: 

Though this is just an idea to the follow up.... I did like the following to fix a header at the top of UITableView:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    self.tableView.sectionHeaderHeight = 40;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 40)];
    return headerView;
}
i wander
+2  A: 

Were it me doing this, I'd take advantage of the fact that UITableViews in the Plain style have the sticky headers and ones in the Grouped style do not. I'd probably at least try using a custom table cell to mimic the appearance of Plain cells in a Grouped table.

I haven't actually tried this so it may not work, but that's what I'd suggest doing.

Colin Barrett
A: 

I add the table to a Scroll View and that seems to work well.

Gary
+1  A: 

Assign a negative inset to your tableView. If you have 22px high section headers, and you don't want them to be sticky, right after you reloadData add:

self.tableView.contentInset = UIEdgeInsetsMake(-22, 0, 0, 0); 
self.tableView.contentSize = CGSizeMake(self.tableView.contentSize.width, self.tableView.contentSize.height+22); 

Works like a charm for me. Works for section footers as well, just assign the negative inset on the bottom instead.

Mike A
+1  A: 

The way I solved this problem is to adjust the contentOffset according to the contentInset in the UITableViewControllerDelegate (extends UIScrollViewDelegate) like this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
       CGFloat sectionHeaderHeight = 40;
   if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
       scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
   } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
       scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
   }
}

Only problem here is that it looses a little bit of bounce when scrolling back to the top.

awulf