2013-04-04 36 views
0

请在xCode中用我的tableView来帮助我。当我推入UITableView中的任何一行时,如何打开另一个viewController?

所以,我有我的代码:

-(void)viewDidLoad { 

    UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, -150, 45)] autorelease]; 

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 400, 40)] autorelease]; 

    headerLabel.text = @"My table"; 

    headerLabel.textColor = [UIColor greenColor]; 

    headerLabel.shadowColor = [UIColor blackColor]; 

    headerLabel.shadowOffset = CGSizeMake(0, 1); 

    headerLabel.font = [UIFont boldSystemFontOfSize:22]; 

    headerLabel.backgroundColor = [UIColor clearColor]; 

    [containerView addSubview:headerLabel]; 

    self.tableView.tableHeaderView = containerView; 

    self.tableData = [NSArray arrayWithObjects:@"Eggs",@"Milk", @"Chocolate", @"Drink", @"Banana", @"Apple", @"Fruit", nil];   
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    //Поиск ячейки 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] autorelease]; 
    } 

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

    return cell; 
} 



    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UIViewController *Main = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"]; 

    [self.navigationController pushViewController:Main animated:YES]; 

    UIViewController *Main2 = [self.storyboard instantiateViewControllerWithIdentifier:@"Main2"]; 
    [self.navigationController pushViewController:Main2 animated:YES]; 

} 

当我推开该行“鸡蛋”将打开另一个的viewController鸡蛋。但我想,当我推动行“牛奶”将打开页面与牛奶,但当我推行@“牛奶”我有页面与“鸡蛋”...感谢您的帮助...非常感谢。我认为它与索引一起工作..但我不知道。

回答

1

试试吧....

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) 
    { 
     UIViewController *Main = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"]; 
     [self.navigationController pushViewController:Main animated:YES]; 
    } 

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

希望它会帮助你....

+0

由于它的工作!谢谢! – 2013-04-04 09:26:36