早教吧 育儿知识 作业答案 考试题库 百科 知识分享

点击不同cell怎样进入不同viewcontroller

题目详情
点击不同cell怎样进入不同viewcontroller
▼优质解答
答案和解析
实现UITableView中点击不同cell进入不同Controller的方法
1>定义一个NSMutableArray的变量,如NSMutableArray *menuList;
2>将要加入cell的对象以字典的方式加入menuList中.
self.menuList = [NSMutableArray array];
//for showing various UIButtons:
ButtonsViewController *buttonsViewController = [[ButtonsViewController alloc] init];
[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"ButtonsTitle",@""),kTitleKey,
NSLocalizedString(@"ButtonsExplain",@""),kExplainKey,
buttonsViewController,kViewControllerKey,
nil]];
[buttonsViewController release];
//for showing various UIControls:
ControlsViewController *controlsViewController = [[ControlsViewController alloc] init];
[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"ControlsTitle",@""),kTitleKey,
NSLocalizedString(@"ControlsExplain",@""),kExplainKey,
controlsViewController,kViewControllerKey,
nil]];
//for showing various UITextFields:
TextViewController *textFieldViewController = [[TextViewController alloc] init];
[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"TextFieldTitle",@""),kTitleKey,
NSLocalizedString(@"TextFieldExplain",@""),kExplainKey,
textFieldViewController,kViewControllerKey,
nil]];
[textFieldViewController release];
3.通过objectforKey取值.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *targetViewController = [[self.menuList objectAtIndex:indexPath.row] objectForKey:kViewControllerKey];
[[self navigationController] pushViewController:targetViewController animated:YES];
}