我正在尝试在Xcode 4.2上创建一个UITableView
应用程序。我只想让每个单元格(例如Cali)在触摸时推送一个新的UIViewController
。
我遇到的问题是,每当我按下单元格时,它都没有按下新的视图控制器。
当我在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 :(得分:0)
首先,TableViewCell具有textLabel属性,因此您无需创建自己的标签。
cell.textLabel.text= [states objectAtIndex:indexPath.row];
是否有理由取消选择单元格?尝试在使用indexPath后取消选择它 并尝试:
if([states objectAtIndex:indexPath.row] isEqualToString:@"cali"]];){
}