【问题标题】:Table view won't push to next view表格视图不会推送到下一个视图
【发布时间】:2013-12-23 23:19:08
【问题描述】:

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

- (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];
}

【问题讨论】:

  • 你做错了几件事。您不应该从单元格中获取数据——单元格用于显示数据,而不是用于提供数据。您应该从用于填充表格的数组中设置 cellText 的值。其次,您不应该使用 alloc init 来获取 ShiftCalculatorViewController 的实例,除非您已经在代码中创建了它的视图(没有 xib 或情节提要)。您应该使用 initWithNibName:bundle:(如果您在 xib 中制作)或 instantiateViewControllerWithIdentifier:(如果在情节提要中制作)。
  • dropDownCellSelected 到底是做什么的?

标签: ios objective-c xcode uitableview xcode5


【解决方案1】:

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

[self.tableView setDelegate:self]

【讨论】:

    【解决方案2】:

    确保您在应用程序委托中编写了导航控制器代码,并且在当前视图控制器中设置了 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.
    

    【讨论】:

      猜你喜欢
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多