2012-01-08 86 views
0

我正試圖在Xcode 4.2上創建一個UITableView應用程序。我只想讓每個細胞(例如Cali)在觸摸時推送新的UIViewController無法從UITableView單元中推送新的視圖控制器

我遇到的問題是每當我按下單元格它不推動新的視圖控制器。

當我把一個破發點的didSelectRowAtIndexPath方法,我看到5個線程。

這裏是我的代碼:

#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 

回答

0

首先TableViewCell有一個爲textLabel屬性,因此你不需要創建自己的標籤。

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

,是有原因的取消小區嘗試取消選中它,你使用indexPath 後嘗試:

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

} 
相關問題