Josh Kahane
Josh Kahane

Reputation: 17169

Scroll UITableView Header?

I have a custom UITableView header which I want to be able to scroll above the top of the table. Normally the header would stick to the top of the table, is it not possible to somehow change to scrolling ability so that it can go beyond that and scrolls with the cells in the table?

Thanks.

Upvotes: 2

Views: 9155

Answers (7)

uthra
uthra

Reputation: 1

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
    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);
    }
}

Upvotes: -2

msmq
msmq

Reputation: 1328

You should use Grouped TableView Style and in your ViewDidLoad method, add following line of code:

myTableView.backgroundColor=[UIColor clearColor];
myTableView.opaque=NO;
myTableView.backgroundView=nil;

Also in nib file, you should clear background color of grouped table;

Upvotes: 0

Tayyab
Tayyab

Reputation: 432

myTableView.tableViewHeader = myCustomTableHeaderView;

This would set myCustomTableHeaderView as the header of your table view and it would scroll with the table view.

Upvotes: 4

Kristof Van Landschoot
Kristof Van Landschoot

Reputation: 1455

You might, like me, have been searching for the tableHeaderView property. See this SO question for more info.

Upvotes: 0

Roy
Roy

Reputation: 3634

By implementing tableView:viewForHeaderInSection: for section of index 0, you can instead have the header as the first section that should disappear when it scrolls. In this way, it's not a header for the whole UITableView.

Upvotes: 1

MGA
MGA

Reputation: 3751

If you only have one header for the whole table, can't you just set the tableHeaderView property?

Upvotes: 0

aqs
aqs

Reputation: 5682

Well if that is the desired behavior you want, just put the header as the first cell's content. It is possible to customize any particular cell you want. UITableViewDelegate documentaion will help you in that matter.
PS: the whole point of using tableView header is to make it stick to the top of the window.

EDIT: If it is necessary that you have to do the way you want, then you can try this: move your tableView a little down by setting its contentOffset. eg: myTableView.contentOffset= CGPointMake(0,heightOfYourView) . Now add yourView at the top

Upvotes: 4

Related Questions