使用另一个视图为每个UITableViewCell创建一个链接

时间:2013-09-23 14:24:30

标签: iphone ios uitableview view cell

我是iOS编程的新手,我想做一件简单的事情。我看到了几个关于我的问题的主题,但我不明白为什么我的代码不起作用......

  • 我创建了一个名为details2ViewController的UIViewController子类,其中包含.xib
  • 在我的主视图中,名为 ViewController.xib ,我有一个tableView
  • ViewController.m 中,我添加了顶部:#import "details2ViewController.h"
  • ViewController.m 中,我修改了 didSelectRowAtIndexPath 方法,如下所示:

    details *det = [[details alloc] init];
    [self.navigationController pushViewController:det animated:YES];
    

没有警告,但是当我点击一个单元格时没有效果......我确切地说我没有使用mainStoryBoard工作。

注意:Here是我之前关于此问题的帖子。

(对不起,如果我的英语很尴尬,我就是法国人......谢谢你的帮助!)

3 个答案:

答案 0 :(得分:0)

首先检查您是否已将TableView的委托正确连接到ViewController的文件所有者。 TableView的-didSelectRowAtIndexPath方法是委托方法。

其次,我不知道为什么在导入details时使用details2ViewController.h作为类名。因此,看起来您应该使用details2ViewController代替details,您的代码应该如下所示:

 details2ViewController *det = [[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil];
[self.navigationController pushViewController:det animated:YES];

确保您的ViewController确实已嵌入UINavigationController。如果没有,则self.navigationController willnil

或者您可以浏览An Introduction To UINavigationController

答案 1 :(得分:0)

如果您使用的是Storyboard,则可以使用此代码。

- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YourViewController *yourViewController = [storyboard instantiateViewControllerWithIdentifier:@"StoryBoardID"];
[self.navigationController pushViewController:yourViewController animated:YES];
}

不要忘记在最后一行将animated:BOOL设置为YES

答案 2 :(得分:0)

您必须先在AppDelegate.h中定义导航控制器的属性:

@property (strong, nonatomic) UINavigationController *controller;

然后在AppDelegate.m文件中,在didFinishLaunchingWithOptions:方法中执行此操作:

details2ViewController *controller=[[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil];
    self.controller = [[UINavigationController alloc] initWithRootViewController:controller];
}

self.window.rootViewController=self.controller;
[self.window makeKeyAndVisible];
return YES;

然后在didSelectRowAtIndexPath中的details2ViewController.m中:

details2ViewController *det = [[details2ViewController alloc] initWithNibName:@"details2ViewController" bundle:nil];
[self.navigationController pushViewController:det animated:YES];

请告诉我它是否有效.. :)谢谢。