2014-01-25 52 views
0

我正在編寫一個程序,該組件使用單個組件實現UIPickerView,以從兩個數組(日期和月份)中選擇和顯示數據。第一個數組包含3行,第二個數組包含12個。程序首先顯示包含三個項目的數組。當我選擇'Month'按鈕以顯示所述第二陣列中,只有前三個月顯示,而不是所有12.在一個UIPickerView組件中顯示來自2個數組的數據

如果我選擇'Time-of-Day'按鈕時,程序崩潰,這表明:

"*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** `-[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'"`. 

我我不確定爲什麼它會被讀取超出數組大小。

任何人可以幫助我明白我需要做的:

  1. 糾正我的代碼,允許選擇器視圖來顯示所有12個月,在「月」按鈕被選中
  2. 防止從程序在顯示月份之後選擇'Time-Of-Day'按鈕時發生崩潰。

以下是代碼...

部首

#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 

實施

#import "ViewController.h" 

@interface ViewController() 
@end 

@implementation ViewController 

@synthesize todArray, monthArray; 
@synthesize thePickerView; 
@synthesize monthTxtField, todTxtField; 

int buttonTouched = 0; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.todArray = [[NSArray alloc] initWithObjects:@"AM", @"Mid-Day", @"PM", nil]; 
    self.monthArray =[[NSArray alloc] initWithObjects: @"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil]; 
} 

- (void) viewDidAppear:(BOOL)animated 
{ 
    //set default rows of UITextFields to an initial state 
    monthTxtField.text = @"January"; 
    todTxtField.text = @"Mid-Day"; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark Button Actions 

- (IBAction)todBtnAction:(id)sender 
{ 
    buttonTouched = 0; 
    [thePickerView reloadAllComponents]; 
} 

- (IBAction)monthBtnAction:(id)sender 
{ 
    buttonTouched = 1; 
    [thePickerView reloadAllComponents]; 
} 

#pragma mark Picker 

// returns the number of components to be used 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 


// returns the # of rows in the components 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:NSInteger)component 
{ 
    switch (buttonTouched) 
    { 
     case 0: 
      return [todArray count]; 
      break; 
     default: 
      return [monthArray count]; 
      break; 
    } 
} 

//Display the row number from the array 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    switch (buttonTouched) 
    { 
     case 0: 
      return [todArray objectAtIndex:row]; 
      break; 
     default: 
      return [monthArray objectAtIndex:row]; 
      break; 
    } 
} 

//Display the row content from the array 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    switch (buttonTouched) 
    { 
     case 0: 
      self.todTxtField.text = [todArray objectAtIndex:row]; 
      break; 
     default: 
      self.monthTxtField.text = [monthArray objectAtIndex:row]; 
      break; 
    } 
} 

@end 
+0

謝謝傑夫。我發現通過切換兩個按鈕動作中的語句順序,使''thePickerView reloadAllComponents''語句出現在buttonTouched語句後,程序正常運行。我也會將你的建議添加到我的代碼中。希望其他人會覺得這很有用,因爲我沒有看到一個與一個UIPickerView組件一起使用的多個數組的完整示例。 –

回答

1

如果在組件0當前所選行大於2時更大你翻轉組件0來表示一個數組機智h 3個成員而不是12個成員的數組,它肯定會崩潰。所以,如果你的月陣列需要調用

[picker selectRow:0 inComponent:0 animated:YES] 

否則,選擇器將嘗試選擇行11月例如它翻轉到每一個有隻有三個,範圍異常後的AM/PM一個翻轉之前time ...

Edit .. SUMMARY .. UIPickerView有一個你沒有考慮過的變量,每個組件當前選中的行。重新加載數據不會改變這個變量,你需要保姆一點..

相關問題