我在使用 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]'
正如我所说,如果在模拟器中测试(没有问题),如果在设备中测试(崩溃)!!!