In my code, three text fields are there for picker views. For example in the first text field, the user selects A+ blood group, in the second text field we don't want to display A+ blood group. Remaining items need to display. Text field 3 also same. For example, in text field 2 the user selects B+ blood group, in text field 3 B+ and A+ blood group needs to remove in array list. For example the user again selects the first text field and reselects another blood group like A- ve group re add the A+ blood group in array list. The same thing in text field2 and text field3. I got some exception. The exception is
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 10 beyond bounds [0 .. 9]'
My json Response is:
question is :({Question = "What is your first pet's name?";QuestionID = 23;},{Question = "Who was your first grade teacher?";QuestionID = 24;},{Question = "What city were you born in?";QuestionID = 25;},{Question = "What was your childhood nickname?";QuestionID = 26;},{Question = "What was the name of your elementary school?";QuestionID = 27;},{Question = "Where does your nearest relative live?";QuestionID = 28;},{Question = "In what city or town was your first job?";QuestionID = 29;},{Question = "Which is your first car? Edited ";QuestionID = 30;},{Question = "This is a test question - answer = ddi";QuestionID = 32;},{Question = "What is your Mother's maiden name?";QuestionID = 33;},{Question = "What is your Last name?";QuestionID = 35;},{Question = "What is the last 4 of your SSN?";QuestionID = 36;},{Question = "What is your Date of Birth? (MM/DD/YYYY)";QuestionID = 37;})
-(void)done:(id)sender
{
int row = [myPickerView selectedRowInComponent:0];
if (isBloodGroupFieldSelected) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:QArray[row] forKey:@"title"];
NSInteger currentRow = row;
[dict setObject:[NSString stringWithFormat:@"%ld",currentRow] forKey:@"selectedIndex"];
[backUpArray replaceObjectAtIndex:0 withObject:dict];
selectedValue1 = QidArray[row];
bloodGroup.text = QArray[row];
[QArray removeObjectAtIndex:row];
[QidArray removeObjectAtIndex:row];
selectedItem1=bloodGroup.text;
NSLog(@"Selected row value 1 of Picker view is:%@",selectedItem1);
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:QArray[row] forKey:@"title"];
NSInteger currentRow = row;
[dict setObject:[NSString stringWithFormat:@"%ld",currentRow] forKey:@"selectedIndex"];
[backUpArray replaceObjectAtIndex:1 withObject:dict];
txtField1.text = QArray[row];
selectedItem2 = txtField1.text;
selectedValue2 = QidArray[row];
[QArray removeObjectAtIndex:row];
[QidArray removeObjectAtIndex:row];
}
else if(!isGenderGroupFieldSelected)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:QArray[row] forKey:@"title"];
NSInteger currentRow = row;
[dict setObject:[NSString stringWithFormat:@"%ld",currentRow] forKey:@"selectedIndex"];
[backUpArray replaceObjectAtIndex:2 withObject:dict];
txtField2.text= QArray[row];
selectedItem3= txtField2.text;
selectedValue3 = QidArray[row];
NSLog(@"Selected row value 3 of Picker view is.....:%@",selectedValue3);
NSLog(@"selected item value 1 of picker view is:%@",selectedItem3);
[QArray removeObjectAtIndex:row];
[QidArray removeObjectAtIndex:row];
}
[myPickerView selectRow:0 inComponent:0 animated:YES];
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
eturn [QArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [QArray count];
}
else if(!isGenderGroupFieldSelected)
{
return [QArray count];
}
return 0;
}
pragma mark - UIPickerViewDelegate
// #5-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return QArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return QArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return QArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
}
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 310, 50)];
lblTitle.numberOfLines=2;
lblTitle.textColor=[UIColor blackColor];
lblTitle.font=[UIFont systemFontOfSize:20];
lblTitle.text=QArray[row];
return lblTitle;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:[backUpArray objectAtIndex:0]];
NSString *str = [dict objectForKey:@"title"]?[dict objectForKey:@"title"]:@"";
NSInteger *index = (int)[NSString stringWithFormat:@"%@",[dict objectForKey:@"selectedIndex"]].intValue;
int count = (int) index;
if(str.length > 0){
[QArray insertObject:str atIndex:0];
}
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:[backUpArray objectAtIndex:1]];
NSString *str = [dict objectForKey:@"title"]?[dict objectForKey:@"title"]:@"";
NSInteger *index = (int)[NSString stringWithFormat:@"%@",[dict objectForKey:@"selectedIndex"]].intValue;
int count = (int) index;
if(str.length > 0){
[QArray insertObject:str atIndex:0];
}
}
else if (textField == txtField2){
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:[backUpArray objectAtIndex:2]];
NSString *str = [dict objectForKey:@"title"]?[dict objectForKey:@"title"]:@"";
NSInteger *index = (int)[NSString stringWithFormat:@"%@",[dict objectForKey:@"selectedIndex"]].intValue;
int count = (int) index;
if(str.length > 0){
[QArray insertObject:str atIndex:0];
}
}
[myPickerView reloadAllComponents];
}
[super viewDidLoad];
backUpArray = [[NSMutableArray alloc]init];
for(int i=0;i<=2;i++){
dict = [[NSMutableDictionary alloc]init];
[backUpArray insertObject:dict atIndex:i];
}
}