2013-01-21 34 views
1

我想鏈接單元格從UITableViewController到UIViewController,但每個單元格都必須調用不同的UIViewController(所以我想象我必須創建一個鏈接到每個視圖控制器),我不知道如何通過故事板鏈接它們。如何將一個UITableViewController鏈接到多個ViewControllers以及如何調用它們

這是我到現在爲止:

MainView.h

@interface MainView : UITableViewController <UITableViewDelegate, UITableViewDataSource> 
{ 
    NSArray *tableData; 
} 
@property (nonatomic, retain) NSArray *tableData; 
@end 

MainView.m

- (void)viewDidLoad 
{ 
    tableData = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil]; 
    [super viewDidLoad]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 
    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"]; 
    } 
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UIViewController *anotherVC = nil; 
    if (indexPath.section == 0) //here I've also tried to use switch, case 0, etc 
    { 
     if (indexPath.row == 0) 
     { 
      anotherVC = [[ViewForFirstRow alloc] init]; 
     } 
    NSLog(@"anotherVC %@", anotherVC); // shows that anotherVC = ViewForFirstRow 
    [anotherVC setModalPresentationStyle:UIModalPresentationFormSheet]; 
    [anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; 
    [self.navigationController pushViewController:anotherVC animated:YES]; 
    } 
} 

@end 

所以,在選擇第一行,我沒有鏈接的UITableViewController到UIViewController(ViewForFirstRow),它顯示一個黑屏。

如果我鏈接的UITableViewController到ViewForFirstRow,它打開視圖,但沒有影響我在代碼[self.navigationController pushViewController:anotherVC animated:YES];使用,這樣看來,它叫做視圖由於連接並沒有因爲代碼[self.navigationController pushViewController:anotherVC animated:YES];

如何我通過代碼正確地調用了視圖,應該在故事板中使用哪種鏈接?另外,如何將UITableViewController鏈接到多個UIViewController(ViewForFirstRow,ViewForSecondRow,ViewForThirdRow等)。

謝謝!

回答

1

在故事板呈現在模態方式的另一個視圖控制器然後使用此:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    Synergy *objSynergy = (Synergy *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Synergy"]; 
[self presentModalViewController:objSynergy animated:YES]; 

Synergy是我的控制器的名字,你可以把這裏要展示你的控制器名稱。

根據您的要求進行更改。而對於不同的視圖 - 控制從不同的行

使用開放式的:

 if (indexPath.section==0) 
      { 
       if (indexPath.row ==0) 
       { 
       } 
      } 
+0

這裏使用不同的行並從該行調用不同的控制器...意味着如果你想訪問你的第二部分和第一行然後右:if(indexPath.section == 1) { if(indexPath.row = = 0) { } } – Vishal

+0

知道了並且努力了!所以我不必在故事板中鏈接它們,對吧?好簡單!我剛剛收到了一個警告'presentModalViewController:animated is deprecated:first deprecated in iOS 6.0',你知道我可以用什麼來代替嗎? – Adami

+0

替換爲以下行:[self presentViewController:pNewController animated:YES completion:nil]; – Vishal

2

首先,你不能掛鉤的故事板從細胞塞格斯(在動態表視圖),你必須做的碼。代碼應該如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if (indexPath.row == 0) { 
     UIViewController *anotherVC = [self.storyboard instantiateViewControllerWithIdentifier:@"anotherVC"]; 
     [self.navigationController pushViewController:anotherVC animated:YES]; 
    }else if (indexPath.row == 1) { 
     UIViewController *someOtherVC = [self.storyboard instantiateViewControllerWithIdentifier:@"someOtherVC"]; 
     [self.navigationController pushViewController:someOtherVC animated:YES]; 
    } 
} 

由於表視圖只有一個部分,所以不應該對indexPath.section做任何事情。

+0

感謝您指出這一點,您是對的。但是現在我會保持這種方式,因爲我打算很快實現可展開的行,並且:) – Adami

相關問題