uncaught exception 'NSRangeException'

246 views Asked by At

I getting crash uncaught exception 'NSRangeException', reason: '*** __boundsFail: index 3 beyond bounds [0 .. 2]' in Objective C

crashlytics issues at this line "label.text = [_addressTypeArray objectAtIndex:row];"

please see the below code and help if possible

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

**- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (isStateButtonClicked == YES) {
        return _stateTypeArray.count;
    } else
        return _addressTypeArray.count;
}

#pragma mark - UIPickerViewDelegate

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc]init];
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:kGothamMediumFontName size:17.0];
    
    if (isStateButtonClicked == YES) {
        NSString *stateName = [NSString stringWithFormat:@"%@ - %@", [[_stateTypeArray objectAtIndex:row] objectForKey:@"StateName"], [[_stateTypeArray objectAtIndex:row] objectForKey:@"StateFullName"]];
        label.text = stateName;
    }
    else {
        

            label.text = [_addressTypeArray objectAtIndex:row];
    }
    
    return label;
}




- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == self.addressTypePickerView)
    {
        self.addressTypePickerView.hidden = NO;
        orderPickerContainerView.hidden = NO;
        disableView.hidden = NO;
        
        if (isStateButtonClicked == YES) {
            self.stateRow = row;
            self.stateTypeSelected = [[_stateTypeArray objectAtIndex:row] objectForKey:@"StateName"];
        } else {
            self.addressTypeRow = row;
            self.addressTypeSelected = [_addressTypeArray objectAtIndex:row];
            
        }
    }
    if (pickerView == self.stateTypePickerView)
    {
        self.stateRow = row;
        self.stateTypeSelected = [[_stateTypeArray objectAtIndex:row] objectForKey:@"StateName"];
    }
    [self.addressTypePickerView reloadAllComponents];
    
    self.scrollViewMain.contentSize = CGSizeMake(self.bounds.size.width, 550.0);
    
}
1

There are 1 answers

0
Helge Becker On

The issue is your button state. It was changed without telling the picker that it has to reload its data. Have a look into the code you call for the button. There should be a line calling reloadallcomponents on the picker.