2013-12-23 41 views
0

我有一个下拉文本框的代码,这样当用户单击表格视图中的一行时,它会推入下一个视图,但由于某种原因,这不会发生。我的代码有问题吗?因为该应用程序不会崩溃,所以我没有错误警告,也不知道为什么它不起作用。表视图不会推送到下一个视图

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 
*)indexPath { 

[delegate dropDownCellSelected:indexPath.row]; 
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
NSString *cellText = selectedCell.textLabel.text; 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setObject:cellText forKey:@"keyToLookupString"]; 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

ShiftCalculatorViewController *shiftCalculatorViewController =  
[[ShiftCalculatorViewController alloc] init]; 
[self presentViewController:shiftCalculatorViewController animated:YES 
completion:nil]; 
} 
+3

你做几件事情是错误的。您不应该从单元获取数据 - 单元格用于显示数据,而不是用于提供数据。您应该从您用来填充表格的数组中设置cellText的值。其次,你不应该使用alloc init来获得ShiftCalculatorViewController的一个实例,除非你已经在代码中做了它的视图(没有xib或storyboard)。你应该使用initWithNibName:bundle :(如果你在xib中创建它),或者使用instantiateViewControllerWithIdentifier :(如果在故事板中创建的话)。 – rdelmar

+0

'dropDownCellSelected'到底做了什么? –

回答

1

这很可能是你没有设置你的UITableView实例的代表,因此任何委托方法如-didSelectRowAtIndexPath不会被调用。

[self.tableView setDelegate:self] 
0

确保你写导航控制器代码的应用程序委托和委托的tableview在当前视图控制器设置好的,然后写这

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // in tableview you will be passing information from an array/dictionary in cell configure method so use the same array/dictionary to get information also. 

[delegate dropDownCellSelected:indexPath.row]; 
NSString *cellText = yourArray[indexPath.row]; 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setObject:cellText forKey:@"keyToLookupString"]; 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

ShiftCalculatorViewController *shiftCalculatorViewController =  
[[ShiftCalculatorViewController alloc] init]; 
[self presentViewController:shiftCalculatorViewController animated:YES 
completion:nil]; 
} 
/// if still app crashes enable exception break point in left menu of your Xcode this will help you to know at which line of code app crashed. 
相关问题