2013-02-11 65 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

相關問題