-2

複数の個別のView Controller(必要)があり、TableViewの各行を個別のView Controllerに接続したいと考えています。

コードに関しては、これは私がこれまでに持っているものです。私はtableViewを作っただけです:

私のViewController.h

[...]
@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
[...]

私のViewController.m

[...]
@implementation SimpleTableViewController
{
NSArray *tableData;
}

[...]

- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
}

[...]

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

[...]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}

また、tableView を dataSource とデリゲートに接続しました。必要なのは、上記の各エントリ (1、2、3) を個別のビュー コントローラーに接続することです。私はすでにすべてのView Controllerを作成しました。

4

2 に答える 2

1

私があなたの質問を正しく理解していれば、didSelectRowAtIndexPath メソッドに if-else または switch ステートメントが必要です。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ViewController1 *vc1 = "instantiate a controller here"
        [self.navigationController pushViewController:vc1 animated:YES];
    else if (indexPath.row == 1) {
        ViewController2 *vc2 = "instantiate a controller here"
        [self.navigationController pushViewController:vc2 animated:YES];
    etc......
于 2013-01-04T18:15:19.407 に答える
0

テーブルビューコントローラーの各行はUITableViewCellであるため、各行の「ビューコントローラー」を制御する場合は、これを参照していると思います。

UITableViewCellをサブクラス化する必要があります。そうすれば、セルを使用するようになったときに、そのサブクラスの新しいインスタンスを作成できます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

編集:UITableViewCellをサブクラス化する必要はありませんが、完全に制御したい場合はそうします。

于 2013-01-04T18:20:01.743 に答える