Objective c 正在修复UITableView的标题

Objective c 正在修复UITableView的标题,objective-c,ios,Objective C,Ios,我想修复UITableView的标题,以便在滚动表视图时标题保持在顶部 我在谷歌上搜索了一下,似乎最干净的方法是使用UINavigationController子类,让它遵循UITableDataSource和UITableViewDelegate协议,在UINavigationController的视图中添加header视图和tableView,并实现所有协议方法 基本上,我做的正是这个问题的答案 我在didSelectRowAtIndexPath方法中有以下内容: - (void)table

我想修复UITableView的标题,以便在滚动表视图时标题保持在顶部

我在谷歌上搜索了一下,似乎最干净的方法是使用UINavigationController子类,让它遵循UITableDataSource和UITableViewDelegate协议,在UINavigationController的视图中添加header视图和tableView,并实现所有协议方法

基本上,我做的正是这个问题的答案

我在didSelectRowAtIndexPath方法中有以下内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease];
    viewController2.title = @"test";
    [self pushViewController:viewController2 animated:YES];
}
如果我在最后一行有self.navigationController,如果这是一个UITableViewController而不是self,那么当然不会发生任何事情,因为self是UINavigationController,但是当我有self时,不会推送viewController@的视图,但是发生了一些事情,因为导航栏随着测试的出现而改变,但固定标题和UITableView仍然存在。更有趣的是,当我第一次点击table视图时,只有test出现在导航栏中,而不是Back按钮。我必须再次点击行,因为在我第一次点击后退按钮以在导航栏中显示后,UITableView仍然存在

简言之

1当我点击表格视图行时,应如何使viewController2的视图显示

2如何让后退按钮在第一次点击时显示

UINavigationController的初始化方法如下:

- (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;
}

注意:如果可能,我希望保留固定标题,即推送的viewcontroller将只占用UITableView的框架。这是因为我的固定标题实际上是导航栏自定义图形的扩展,即导航栏和固定标题组合形成一个完整的图像。Thx这听起来像是分组表视图的默认行为。您应该只需要实现用自定义标题填充标题或覆盖标题的方法,并且在滚动时,这些方法应保持在顶部,直到下一个标题将其向上推。