2

在我的项目中,我有 10 个单元格的 tableView。并且在didSelectRowAtIndexPath所有单元格中都有多个 ViewController (文件)所以,我didSelectRowAtIndexPath看起来像

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

if(indexPath.row == 0) {

            CallViewController *viewc = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil];
            [self.navigationController pushViewController:viewc animated:YES];
        }else if(indexPath.row == 1) {
            BirthdayViewController *viewc = [[BirthdayViewController alloc] initWithNibName:@"BirthdayViewController" bundle:nil];
            [self.navigationController pushViewController:viewc animated:YES];
        }

所以我不希望这些条件我希望我的代码是干净的

4

4 回答 4

3

我建议你有一个包含Class你的类的对象的数组,然后创建对象并像这样推送

//view did load

mutableArr = [[NSMutableArray alloc] init];
[mutableArr addObject:[CallViewController class]];
[mutableArr addObject:[BirthdayViewController class]];
....
....

然后在你的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (indexPath.row < [mutableArr count]) {
       Class *obj = [mutableArr objectAtIndex:indexPath.row];
       UIViewController *controller = [[objc alloc] initWithNibName:NSStringFromClass(obj) bundle:nil];
       [self.navigationController pushViewController:controller animated:YES];
   }
}

或者,如果这看起来更奇怪,那么你可以这样做

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UIViewController *controller = nil;
   switch(indexPath.row) {
       case 0:
          controller = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil];
          break;
       case 1;
          controller = [[BirthdayViewController alloc] initWithNibName:@"BirthdayViewController " bundle:nil];
          break;
   }
    [self.navigationController pushViewController:controller animated:YES];
}
于 2012-12-20T12:00:02.657 回答
2

你的 tableview 底层数据模型是什么?如果您可以添加其他属性作为 ViewController 类名称,您可以执行类似的操作

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *row = [rowData objectAtIndex:indexPath.row];
Class viewControllerClass =
    NSClassFromString([row objectForKey:@"viewControllerClassName"]);

//Default VC init looks for nib file named as VC afaik, if not, you could
    //add another attribute with init selector name
UIViewController *viewController =
    [[viewControllerClass alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
}
于 2012-12-20T11:59:28.710 回答
1

你可以这样做:

将所有类添加到 NSArray,并通过 indexPath.row 选择正确的类。

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      NSArray * conrollersClasses =@[[CallViewController class],[BirthdayViewController class]];
      UIViewController *controller = [[conrollersClasses[indexPath.row] alloc] init];
      [[self navigationController] controller animated:YES];

}
于 2012-12-20T12:04:22.610 回答
1

您可以创建对应于文件名的字符串数组并使用 NSClassFromString 函数来分配视图控制器

NSArray *viewControllers = [NSArray arrayWithObjects:@"VC1", @"VC2", @"VC3", nil];
id viewController = [[NSClassFromString([viewControllers objectAtIndex:indexPath.row]) alloc] initWithNibName:[viewControllers objectAtIndex:indexPath.row] bundle:nil];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
于 2012-12-20T11:59:44.327 回答