2013-01-04 42 views
-2

我有多个单独的视图控制器(我需要),并希望将TableView中的每一行连接到单独的视图控制器。将UITableView单元连接到单独的视图控制器

至于代码,这是我到目前为止。我只是做了的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到数据源和委托。我需要的是将上面的每个条目(一个,两个,三个)连接到单独的视图控制器。我已经制作了所有的视图控制器。

回答

1

如果我正确理解你的问题,你只需要的if-else或switch语句在您的didSelectRowAtIndexPath方法方法:

- (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...... 
0

表视图控制器中的每一行是一个UITableViewCell,所以我猜那是什么你指的是何时想要控制每一行的'视图控制器'。

您需要继承的UITableViewCell,然后当你来用细胞在

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

编辑您可以创建一个子类的一个新实例:就算你不要有子类的UITableViewCell,但如果你想完全控制它然后你做。

相关问题