0

I want to fix the header for a UITableView such that the header sticks to the top when the table view is scrolled.

I did some googling and it seems that cleanest way is to use subclass a UINavigationController and make it follow the UITableDataSource and UITableViewDelegate protocols, and the in the view of the UINavigationController add the header view and the tableView and implement all the protocol methods.

basically I am doing exactly as the chosen answer for this stackoverflow question

I have in the didSelectRowAtIndexPath method the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease];
    viewController2.title = @"test";
    [self pushViewController:viewController2 animated:YES];
}

If I have in the last line "self.navigationController" (as one normally would if this is a UITableViewController) instead of "self", nothing happens (of course, since self is the UINavigationController) but when I have "self" as it currently is, the view of the viewController@ is not pushed, however something is happening because the navigation bar has changed with "test" appearing, but the fixed header and UITableView remains. More interestingly, when I tap the table view for the first time, only "test" appear in the navbar but not the "Back" button. I have to tap the rows another time (since the UITableView remains after I tap the first time) for the "Back" button to show in the navbar...

so in short

1) what should I do to make the view of viewController2 shows when i tap on the table view row?

2) how do I get the "Back" button to show at the first tap?

my init method of the UINavigationController is as follow:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.navigationController setNavigationBarHidden:NO];

        UIView *mainView = [[[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight+navBarHeight, 320, 480-statusBarHeight-navBarHeight-tabBarHeight)] autorelease];

        UIImage *stripedBackground = [UIImage imageNamed:@"bg.png"];
        UIImageView *background = [[[UIImageView alloc] initWithImage:stripedBackground] autorelease];
        background.frame = CGRectMake(0, 0, 320, mainView.frame.size.height);
        [mainView addSubview:background];

        UIImage *header = [UIImage imageNamed:@"header.png"];
        UIImageView *headerView = [[[UIImageView alloc] initWithImage:header] autorelease];
        headerView.frame = CGRectMake(0, 0, header.size.width, header.size.height);
        [mainView addSubview:headerView];

        UITableView *streamTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, header.size.height, 320, mainView.frame.size.height-header.size.height) style:UITableViewStylePlain] autorelease];
        [streamTableView setDelegate:self];
        [streamTableView setDataSource:self];
        [mainView addSubview:streamTableView];
        [header release];

        [self.view addSubview:mainView];
    }
    return self;
}
Community
  • 1
  • 1
minovsky
  • 787
  • 14
  • 28
  • **NOTE**: if possible I would like to keep the fixed header as well (i.e. the pushed viewcontroller will only occupy the frame of the UITableView). this is because my fixed header is actually an extension of the custom graphic I have for my navigation bar, i.e. the navigation bar and the fixed header combined form one complete image. thx – minovsky Jun 22 '12 at 10:22
  • That sounds like default behavior for grouped tableviews. You should only need to implement the methods to fill the headers (or override with your custom header) and they should stick at the top while scrolling until the next header pushes it up. – Bill Burgess Jun 22 '12 at 13:48

0 Answers0