如何从UITableView对象单元推送新的视图控制器

时间:2012-01-02 05:56:31

标签: uitableview ios5 xcode4.2 pushviewcontroller

我正在尝试在Xcode 4.2上创建一个UITableView应用程序。 我只是希望每个单元格(例如Cali)在推送时推送新的ViewController

我遇到的问题是每当我按下单元格而不是按下新的视图控制器

我的TableViewController.h

#import <UIKit/UIKit.h>

@interface Adam : UITableViewController 
{
    NSMutableArray *states;
}

@end

我的Tableviewcontroller.m

#import "Adam.h"
#import "ViewController.h"


@implementation Adam


- (void)viewDidLoad
{
    [super viewDidLoad];

    states = [NSMutableArray arrayWithObjects:
              @"cali",
              @"ohio",
              nil];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

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

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

    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    [cellLabel setText:[states objectAtIndex:indexPath.row]];    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([[states objectAtIndex:indexPath.row] isEqual:@"cali"])

    { 
        ViewController *cali = [[ViewController alloc] initWithNibName:@"cali" bundle:nil]; 
         [self.navigationController pushViewController:cali animated:YES];

    }
}

@end

1 个答案:

答案 0 :(得分:0)

cali.title = [states objectAtIndex: [indexPath row]];
[self.navigationController pushViewController:cali animated:YES];