当我按下UITableView中的任何一行时,如何打开另一个viewController?

时间:2013-04-04 08:44:15

标签: xcode uitableview

请使用xCode中的tableView来帮助我。

所以我有我的代码:

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

}

当我按下行时,“Eggs”会打开另一个带有鸡蛋的viewController。但我想要的是,当我推动排“牛奶”将打开牛奶页面,但当我推开行@“牛奶”我有“鸡蛋”的页面...感谢您的帮助...非常感谢。我认为它适用于索引..但我不知道。

1 个答案:

答案 0 :(得分: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];
    }
}

希望它能帮到你......