Search code examples
iosxcodeuipickerview

Disable row 0 on selection UIPickerview - iOS


I'm trying to disable row 1 on UIPickerView, but it throws me the following error

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 15]'

Anyone can suggest me what went wrong here? Here is the code

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{   
   AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   if ((row > 0)){
        NSLog(@"%@",[locationArray objectAtIndex:row]);

        if(flag){

        appdelegate.selectedLocation = [locationArray objectAtIndex:row];
        NSLog(@"%@",appdelegate.selectedLocation);

       }  else{

        appdelegate.selectedLocation = [locationArray objectAtIndex:row];
        NSLog(@"%@",appdelegate.selectedLocation);

    }
        [promptLocation addSubview:tapButton];
        [tapButton addTarget:self action:@selector(goToDesireLoc:) forControlEvents:UIControlEventTouchUpInside];
   }else{
    //Don't do anything if row 0 is chosen.
    NSLog(@"do nothing");
   }
}

Solution

  • I would suggest that you add exception breakpoints so that you can see what line of your code is causing the crash. See here for how to do it: stackoverflow.com/q/4961770/472344.

    This should cause your code to break at the line that is causing the crash. You can check the value of "row" and the size of "locationArray".

    It seems though that your row is 2147483647 and your array is of length 16. So I guess there may be a problem with your UIPickerView dataSource. Maybe post your code for:

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    

    You could also set a breakpoint inside that method to make sure that it's getting called correctly, and to check the values that it is returning.