views:

385

answers:

2

First a little background info:

I have UIViewController that contains a UITableView. In the loadView method (after initialization of the table), I set the UIViewControllers view to the table view with: self.view = tableView;

What I want is a view on the top of the screen (before the UITableView), that doesn't scroll with the rest of the table view when it is scrolled. I have tried adding my UIView to the table view's tableViewHeader, which displays correctly but scrolls with the rest of the table.

Is there any easy fix for this? Either way, any hints towards a solution is greatly appreciated.

EDIT:

Come to think of it, what I want is something like the stock application where the bottom part is stationary and the rest of the screen is a UITableView. The only difference is that I want the stationary part at the top of the screen.

A: 

Is there a reason you are setting the view of the UIViewController to that of the UITableView? Why not handle the UITableView as a subview? That would allow you to add anything you want above the UITableView -another view, empty space with the view of the UIViewController as your background, etc.

Mark Hammonds
What do you suggest? I have tried adding the UIView and the UITableView to the UIViewController by saying: [self.view addSubview:headerView]; and [self.view addSubview:tableView]; Each view have been initialized with an appropriate frame. When I do this the program crashes and the debugger launches displaying: Loading 95186 stack frames...
Erik
Hmm, found the error. I tried your approach earlier and, as I said, the whole thing crashed. That was because I had forgot to put [super loadView]; in my loadView method. So I'll give you the correct answer on this, because your approach is what I'll end up using in the end.
Erik
A: 

As kmit has already pointed out, you can easily add more than one subview to your view. So, don't set the table view directly as self.view, but rather create a blank UIView (as container) and add the table view as well as the header view as subviews to that view. You can control the views' extents via their frame attributes. A simple example:

- (void)loadView {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

    // header view
    HeaderView* headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 182)];
    self.headerView = headerView;   // in case you need the reference later on
    [view addSubview:headerView];
    [headerView release];

    // table view
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 182, 320, 186) style:UITableViewStylePlain];
    [tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
    tableView.delegate = self;
    tableView.dataSource = self;
    [view addSubview:tableView];
    self.tableView = tableView;
    [tableView release];

    self.view = view;
    [view release];
}

As an alternative to creating the containing UIView manually, you can call [super loadView] at the beginning of your loadView implementation.

Daniel Rinser