iphone UITableVIew(基于导航)

时间:2011-05-01 12:46:28

标签: iphone uitableview xcode4 navigationcontroller

我正在开发基于导航的应用程序。 我的rootViewController包含3个单元格 - 当按下第一个时,按下UIViewController(这个工作) - 问题在于第二和第三个单元应该推送一个UITableViewController

应用程序运行时没有任何错误,它根本没有崩溃,但是当我导航到tableview时,会查看一个没有元素的空表。

这部分代码存在问题吗? :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"Introduction" bundle:nil];
    detailViewController.title=@"Introduction";

    UITableViewController *myPictures = [[UITableViewController alloc] initWithNibName:@"Pictures" bundle:nil];
    myPictures.title=@"Pictures";

    UITableViewController *myImages = [[UITableViewController alloc] initWithNibName:@"ImagesViewController" bundle:nil];
    myImages.title=@"Images";

    // Pass the selected object to the new view controller.
    if (0 == indexPath.row)
    [self.navigationController pushViewController:detailViewController animated:YES];

    if (1== indexPath.row)
    [self.navigationController pushViewController:myPictures animated:YES];

    if (2== indexPath.row)
    [self.navigationController pushViewController:myImages animated:YES];

    [detailViewController release];
    [myPictures release];   
    [myImages release];

1 个答案:

答案 0 :(得分:0)

你做错了什么(除了原来的问题)。为什么要实例化每个视图控制器,然后仅使用基于当前“单元格选择”的视图控制器?这会降低您的应用程序速度,具体取决于加载每个单独视图所需的时间。您只应在里面的相关视图控制器

除此之外,你的方法有很多不妥之处。你正在做什么将永远不会工作,因为实例化一个通用的UITableViewController(即使你提供自己的笔尖)显然只会显示一个空视图。你需要拥有一个自己的类,nib被绑定为一个委托,然后它将为TableView提供数据。

我相信当你创建这些nib文件时(例如“图片”),xcode也会给你一个'PicturesViewController.h和PicturesViewController.m'文件?如果是这样,你需要在那里编写相应的代码并确保Tableview在“Pictures”nib文件的'datasource'和'delegate'设置为'PicturesViewController'。然后当你想要显示该视图时:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 1) {
        ...
    } else if (indexPath.row == 2) {
     PicturesViewController *myPictures = [[PicturesViewController alloc] initWithNibName:@"Pictures" bundle:nil];

     [self.navigationController pushViewController:myPictures animated:YES];
     [myPictures release];
    }

}