Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i make a table's tableHeaderView position fixed as I scroll?

I have a table view and have attached to its tableHeaderView a UISegmentedControl. How can I make the tableHeaderView fixed so that i can always view the UISegmentedControl in the same position even when i am scrolling the table view?

like image 680
Alex Avatar asked Sep 15 '11 15:09

Alex


4 Answers

To expand on @mskw's answer:

You can use the UITableViewController (keep the niceties such as UIRefreshControl support and keyboard avoidance). You just have to embed your toolbar in a plain view and place that in your tableHeaderView. Then implement this scroll view delegate method to lock.

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect rect = self.toolbarContainerView.frame;
    rect.origin.y = MIN(0,scrollView.contentOffset.y + scrollView.contentInset.top);
    self.toolbarContainerView.frame = rect;
}

Note that if you also use section headers you will have to send those views behind your tableHeaderView otherwise they will float over the tableHeaderView.

like image 200
Steve Moser Avatar answered Oct 04 '22 00:10

Steve Moser


tableView:ViewForHeaderInSection: is your option to achieve your task. In plain table this will somehow looks like Address Book app with first char of name in section, but you will have your segmented control

like image 7
Serhii Mamontov Avatar answered Nov 04 '22 06:11

Serhii Mamontov


I would suggest placing the UISegmentedControl in a separate view on top of the UITableView rather than in the tableHeaderView. You might also want to set yourTable.bounces = NO; in order to keep the header view from bouncing when you get to the top of the table.

like image 1
Mark Armstrong Avatar answered Nov 04 '22 05:11

Mark Armstrong


First lower the tableview's frame(make space on top of tableview). Then during scrollViewDidscroll, fix the tableviewheader, properly, so it doesn't scroll with it.

Thats the summary of it.

like image 1
mskw Avatar answered Nov 04 '22 04:11

mskw