2013-02-11 64 views
1

我现在正在用tableview创建一个应用程序现在我想要发生的是当我点击第一行时它将显示view1,当我点击第二行时它将显示view2。我怎样才能做到这一点?tableview根据点击的行打开不同的视图

我试图使用if else语句ID我didSelectRowAtIndexPath,但它并没有在这里工作是我的一段代码

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

int EntryIndex = [indexPath indexAtPosition:[indexPath length]-1]; 

{ 
    AboutViewController *aboutView = [[AboutViewController alloc]init]; 
    aboutView.getId4 = [[contentArray objectAtIndex:EntryIndex]objectForKey:@"title"]; 
    aboutView.aboutofficeId = getId3; 
    [self.navigationController pushViewController:aboutView animated:YES]; 
} 
else 
{ 
    DepartmentsViewController *deptView = [[DepartmentsViewController alloc]init]; 
    deptView.getId5 = [[contentArray objectAtIndex:EntryIndex]objectForKey:@"title"]; 
    deptView.deptoffice = getId3; 
    [self.navigationController pushViewController:deptView animated:YES]; 
} 

} 
+0

üWnt信号添加的UIView或UIViewController中>> – iPatel 2013-02-11 10:21:00

+0

我想添加一个新的UIViewController :) – 2013-02-12 01:55:32

回答

0

注意:您要创建和显示的UIView,所以我在这里推杆UIViewUIViewController

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

     if(indexPath.row == 1) 
     { 
      UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake("AsYouWnat")]; 
      view1.backgroundColor = [UIColor redColor]; 
      [self.view addSubview:view1]; 
     } 
     else if(indexPath.row == 2) 
     { 
      UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake("AsYouWnat")]; 
      view2.backgroundColor = [UIColor redColor]; 
      [self.view addSubview:view2]; 
     } 
     . 
     . 
     . 
     . 
     . 
     ////////// As Your Row In Table 
     else 
     { 
      UIView *viewN = [[UIView alloc] initWithFrame:CGRectMake("AsYouWnat")]; 
      viewN.backgroundColor = [UIColor redColor]; 
      [self.view addSubview:viewN]; 
     } 
    } 
+0

你能告诉我我如何显示不同的UIViewcontrollers? – 2013-02-12 02:25:30

+1

简单创建UIView的ViewController实例。 :)如AboutViewController * aboutView = [[AboutViewController alloc] init]; aboutView.getId4 = [[contentArray objectAtIndex:EntryIndex] objectForKey:@“title”]; aboutView.aboutofficeId = getId3; [self.navigationController pushViewController:aboutView animated:YES]; – iPatel 2013-02-12 04:09:41

+0

谢谢!这会做现在:) – 2013-02-12 05:39:35

0
  1. 如果你希望显示视图可以声明可变数组,并添加UIviews中,如果UIView的内容有不同类型的内容风格

  2. 如果您需要显示相同样式的视图,但只有内容会有所不同。您可以查找以下代码。

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)]; 
    switch (indexPath.row) { 
    case 0: 
    { 
    //Add your label,image,etc 
    [self.view addSubview:view]; 
    } 
        break; 
    
        case 1: 
    { 
    
    //Add your label,image,etc 
    [self.view addSubview:view]; 
    
    } 
        break; 
    
  3. 如果你想显示不同的ViewControllers然后ü需要做下面的代码

    #import "ViewController1.h" 
    #import "ViewController2.h" 
    #import "ViewController3.h" 
    
    -(void)viewDidLoad 
    { 
    
    VCArray=[[NSMutableArray alloc]initWithObjects:ViewController1,ViewController2,ViewController3];//allocate and init ViewController and add that to VCArray 
    
    } 
    
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    
    UIViewController *viewcontr=[VCArray objectAtIndex:indexpath.row]; 
    viewcontr=[[viewcontr alloc]init]; 
    [self presentModalViewController:viewcontr animated:YES]; 
    
        } 
    
+0

谢谢!我使用你的第3个答案,但我得到一个错误 [__NSCFConstantString parentViewController]:无法识别选择发送到实例0x1ab7c – 2013-02-12 01:52:49

+0

谢谢!使用3号答案,但IM得到一个错误[__NSCFConstantString parentViewController]林:无法识别的选择发送到实例0x1ab7c – 2013-02-12 02:22:58

+0

@ TBT-lionArmanCapistrano u能提供的代码如何加入到Viewcontrollers阵列 – Dany 2013-02-12 05:03:25

相关问题