selectedRowInComponent在numberOfRowsInComponent中崩溃

时间:2012-09-10 16:32:44

标签: xcode4 uipickerview

我在使用Xcode(4.4.1)

编程创建的pickerView中有2个组件
UIPickerView *picker = [[[UIPickerView alloc] init] retain];
[picker sizeToFit];
picker.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;

我认为不需要转储与此pickerView相关的所有代码,因为我知道它崩溃了。

pickerView就像模拟器中的魅力一样,但是只要我在iPhone设备(5.1.1)中测试它,它就会在以下方法中崩溃:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  if (component == 0) return [array1 count];
  if (component == 1) {
    NSLog(@"selectedRowInComponent:%i",[pickerView selectedRowInComponent:1]); //CRASHES HERE
    return [[array2 objectAtIndex:[pickerView selectedRowInComponent:1]] count];
  }
}

它引发了以下错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

正如我所说,如果在模拟器中测试(没有问题),如果在设备中测试(CRASHES)!!!

2 个答案:

答案 0 :(得分:0)

你确定它在你指示的线路上崩溃而不是它后面的线路吗?

模拟器和经常绊倒人们的真实设备之间的区别在于文件系统在实际设备上区分大小写,但在模拟器中不区分大小写。如果您从文件加载选取器的数据,并且您指定了错误的情况,这可能会导致您的问题。我见过人们多次犯这个错误。查看array2并确保它包含您的想法。

答案 1 :(得分:0)

有同样的问题......仅在ios8上崩溃。 奇怪的是,当我还原组件时,我遇到了这个问题。 (反转0和1)

将[pickerView selectedRowInComponent:1]替换为包含当前所选行的属性,因为存在并发问题