下面是我的代碼
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//indexPath.row得到選中的行號。
NSLog(@"%ld",(long)indexPath.row);
switch (indexPath.row) {
case 0:
{ //初始化界面
HLNoAudit *my = [[HLNoAudit alloc] init];
my.title =[self.list objectAtIndex:0];
//設置返回鍵
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 30, 30);
[back setBackgroundImage:[UIImage imageNamed:@"arrow_left"] forState:UIControlStateNormal ];
[back setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
[back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
my.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
[self.navigationController pushViewController:my animated:YES];
}
break;
case 1:
{
HLAlreadyAudit *my = [[HLAlreadyAudit alloc] init];
my.title =[self.list objectAtIndex:1];
//設置返回鍵
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 30, 30);
[back setBackgroundImage:[UIImage imageNamed:@"arrow_left"] forState:UIControlStateNormal ];
[back setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
[back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
my.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
[self.navigationController pushViewController:my animated:YES];
你選用的方法是錯誤的, 應該用didSelectRowAtIndexPath而不是 didDeselectRowAtIndexPath。使用didDeselectRowAtIndexPath方法第一次點擊選中cell是沒有反應的,當點擊其他的cell之後才取消之前選中的cell,然後才能調用didSelectRowAtIndexPath方法。