我在IB中设置了一些视图控制器。一个具有大约6个单元格的TableView(以编程方式填充)。然后是另外6个视图控制器(每个单元一个)。
我想在我的
中做到这一点- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
我可以将视图切换到相对于单击的单元格的任何视图。
因此,如果他们单击cell1,它将转到view1,如果他们单击cell2,则转到view2。
这就是我用btw启动表格的方法,它运行正常。
//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return showArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.showLabel.text = [showArray objectAtIndex:indexPath.row];
cell.image.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
cell.seasonLabel.text = [seasonArray objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 106;
}
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation{
NSIndexPath *newCellPath = [NSIndexPath indexPathForRow:showArray.count
inSection:0];
[self.theatreView insertRowsAtIndexPaths:@[newCellPath]
withRowAnimation:UITableViewRowAnimationFade];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
答案 0 :(得分:0)
我会根据你的应用程序的结构建议这两种方法中的一种。
[self.navigationController pushViewController:newViewController animated:YES];
[self presentModalViewController:newViewController animated:YES];
//for ios 6.0
[self presentViewController:newViewController animated:animated completion:NULL];
从我可以收集到的内容中,您在主菜单的.xib文件中创建了很多uiviewcontrollers。那是对的吗?所以你将拥有self.theatreViewController。然后,您可以将其作为按钮回调中的参数传递。例如
[self presentViewController:self.theatreViewController animated:animated completion:NULL];
如果我的假设错了,请纠正我。
答案 1 :(得分:0)
我不知道你的应用程序的结构,但我想你可能会考虑这个:
- (UIViewController *)getViewControllerAtIndexPath:(NSIndexPath *)indexPath {
//..you can use "switch" to return the view controller you want
return ...;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = [self getViewControllerAtIndexPath:indexPath];
//..push or present your controller
}
答案 2 :(得分:0)
在此方法中使用索引路径属性: (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
像
yourObject = [yourArray objectAtIndex:indexPath];
编辑: - 在以下方法的AppDelegate中,执行以下操作: -
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
mHomeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mHomeViewController];
[mHomeViewController release];
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[navController release];
return YES;
}
现在,在你的viewController中 - didSelectRowMethod,执行以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * object = [yourArray objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
if(newControllerInstance == nil) // check if it's nil, then initialize and alloc it.
{
newControllerInstance = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.navigationController newControllerInstance animated:YES];
[newControllerInstance release];
}
}
这应该有用。