简体   繁体   中英

Can't push a new view controller from UITableView cell

I am trying to create a UITableView application on Xcode 4.2. I just want each cells (ex. Cali) to push a new UIViewController when it's touched.

The issue I'm running into is whenever I press the cell it's not pushing the new view controller.

When I put a break point at the didSelectRowAtIndexPath method, I see 5 threads.

Here's my code:

#import <UIKit/UIKit.h> 

@interface Adam : UITableViewController
{
    NSMutableArray *states;
}
@end


#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

First the TableViewCell has a textLabel property so you don't need to create your own label.

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

and is there a reason your deselecting the cell?Try deselecting it after you use the indexPath And try:

if([states objectAtIndex:indexPath.row] isEqualToString:@"cali"]];){

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM