在一个UIPickerView组件中显示来自2个阵列的数据
我正在编写一个程序,该程序实现了一个UIPickerView,其中包含一个组件,用于选择和显示两个数组(时间和月份)中的数据。第一个数组包含3行,第二个数组包含12行。程序首先显示包含三项的数组。当我选择“月”按钮显示第二个数组时,只显示前三个月,而不是全部12个月 如果我选择在一个UIPickerView组件中显示来自2个阵列的数据,uipickerview,Uipickerview,我正在编写一个程序,该程序实现了一个UIPickerView,其中包含一个组件,用于选择和显示两个数组(时间和月份)中的数据。第一个数组包含3行,第二个数组包含12行。程序首先显示包含三项的数组。当我选择“月”按钮显示第二个数组时,只显示前三个月,而不是全部12个月 如果我选择“一天中的时间”按钮,程序将崩溃,表明: "*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** `-[__NSArr
“一天中的时间”
按钮,程序将崩溃,表明:
"*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** `-[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'"`.
我不知道为什么它会被读取超过数组大小
有人能帮我理解我需要做什么来:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UITextField *todTxtField;
@property (strong, nonatomic) IBOutlet UITextField *monthTxtField;
@property (strong, nonatomic) NSArray *todArray;
@property (strong, nonatomic) NSArray *monthArray;
@property (strong, nonatomic) IBOutlet UIPickerView *thePickerView;
- (IBAction)monthBtnAction:(id)sender;
- (IBAction)todBtnAction:(id)sender;
@end
如果在翻转组件0以表示包含3个成员的数组而不是包含12个成员的数组时,组件0中当前选定的行大于2,则肯定会崩溃。因此,在您从月份数组切换到am/pm数组之前,您需要调用
[picker selectRow:0 inComponent:0 animated:YES]
否则,选择器将尝试选择12月份的第11行,例如,在它翻转到只有三行之后,每次都会出现范围异常
编辑。。总结。。UIPickerView有一个您没有考虑的变量,即每个组件中当前选定的行。重新加载数据不会改变此变量,您需要稍微照看一下它。谢谢Jef。我发现通过切换两个按钮操作中语句的顺序,“[thePickerView重载所有组件];”语句出现在ButtonTouch语句之后,程序正常运行。我还将把你的建议添加到我的代码中。希望其他人会发现这很有用,因为我还没有看到一个UIPickerView组件使用多个数组的完整示例。
[picker selectRow:0 inComponent:0 animated:YES]