0

请用我tableView的 xCode 帮助我。

所以我有我的代码:

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

}

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

4

1 回答 1

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

希望对你有帮助......

于 2013-04-04T09:01:53.163 回答