1

UITableViewController から UIViewController にセルをリンクしようとしていますが、各セルは異なる UIViewController を呼び出す必要があり (したがって、各ビュー コントローラーへのリンクを 1 つ作成する必要があると思います)、それらをリンクする方法がわかりません。ストーリーボードを通して。

これは私が今まで持っているものです:

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 など) にリンクするにはどうすればよいですか。

ありがとう!

4

2 に答える 2

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]; 
    } 
}

テーブルビューにはセクションが1つしかないため、indexPath.sectionでは何もしないでください。

于 2013-01-21T03:18:38.377 に答える
1

モーダルな方法で別のViewControllerを提示するストーリーボードで、これを使用します:

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

Synergy is my controller name 提示したいコントローラー名をここに入力できます。

要件に合わせて変更してください。そして、別のビューコントローラーが別の行から開いている場合

これを使って:

       if (indexPath.section==0) 
          {
              if (indexPath.row ==0) 
              {
              }
          }
于 2013-01-21T03:09:09.723 に答える