Search code examples
iosobjective-cuitextfielduipickerview

Custom UIPickerView crashes when datasource does not have data


I have a screen with two text fields (category, subcategory), each hooked up to a custom UIPickerView. The options present in the subcategory view depend on the category selected as the value of the first field.

If the user has not selected a category, selecting the subcategory field brings up the standard keyboard (this behavior is fine).

If the user selects a category and then interacts with the subcategory field everything works fine.

The problem happens when the user puts in a category, brings up the subcategory picker, and then goes back and clears the category field. At this point, if the user selects the subcategory field, the picker will appear without any data and interacting with it will cause the app to crash.

The error text:

*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x719eb60> 2 indexes [0, 0])'
*** First throw call stack:
(0x1cc3012 0x1100e7e 0x1cc2e78 0xb96665 0x22df20 0xf12de 0x481086 0x480f7a 0xa440d 0xa69eb 0x30f85a 0x30e99b 0x3100df 0x312d2d 0x312cac 0x30aa28 0x77972 0x77e53 0x55d4a 0x47698 0x1c1edf9 0x1c1ead0 0x1c38bf5 0x1c38962 0x1c69bb6 0x1c68f44 0x1c68e1b 0x1c1d7e3 0x1c1d668 0x44ffc 0x2acd 0x29f5)
libc++abi.dylib: terminate called throwing an exception

Here is my code:

- (IBAction)showYourPicker:(id)sender {
        isCategoryPicker = true;
        // create a UIPicker view as a custom keyboard view
        UIPickerView* pickerView = [[UIPickerView alloc] init];
        [pickerView sizeToFit];
        pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.showsSelectionIndicator = YES;
        self.catPickView = pickerView;  //UIPickerView

        categoryField.inputView = pickerView;

        // create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
        // Prepare done button
        UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
        keyboardDoneButtonView.barStyle = UIBarStyleBlack;
        keyboardDoneButtonView.translucent = YES;
        keyboardDoneButtonView.tintColor = nil;
        [keyboardDoneButtonView sizeToFit];

        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                        style:UIBarButtonItemStyleBordered target:self
                                                                       action:@selector(pickerDoneClicked:)];

        [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];

        // Plug the keyboardDoneButtonView into the text field...
        categoryField.inputAccessoryView = keyboardDoneButtonView;
    }

    - (IBAction)showYourSubPicker:(id)sender {
        isCategoryPicker = false;
        WCSharedCache *sharedManager = [WCSharedCache sharedManager];
        BOOL iLLAllowIt = false;
        for(int i = 0; i < [sharedManager.categories count]; i++) {
            if([[[sharedManager categories] objectAtIndex:i] isEqualToString:[categoryField text]]) {
                iLLAllowIt = true;
            }
        }
        if(!iLLAllowIt) {
            return;
        }
        // create a UIPicker view as a custom keyboard view
        UIPickerView* pickerView = [[UIPickerView alloc] init];
        [pickerView sizeToFit];
        pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.showsSelectionIndicator = YES;
        self.subCatPickView = pickerView;  //UIPickerView

        subcategoryField.inputView = pickerView;

        // create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
        // Prepare done button
        UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
        keyboardDoneButtonView.barStyle = UIBarStyleBlack;
        keyboardDoneButtonView.translucent = YES;
        keyboardDoneButtonView.tintColor = nil;
        [keyboardDoneButtonView sizeToFit];

        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                       style:UIBarButtonItemStyleBordered target:self
                                                                      action:@selector(pickerDoneClicked:)];

        [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];

        // Plug the keyboardDoneButtonView into the text field...
        subcategoryField.inputAccessoryView = keyboardDoneButtonView;
    }

    - (void) pickerDoneClicked: (id) picker {
        if(isCategoryPicker) {
            [categoryField resignFirstResponder];
        } else {
            [subcategoryField resignFirstResponder];
        }
    }

    - (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        WCSharedCache *sharedManager = [WCSharedCache sharedManager];
        if(isCategoryPicker) {
            [categoryField setText:[[sharedManager categories] objectAtIndex:row]];
        } else {
            @try {
                int idx = 0;
                for(int i = 0; i < [sharedManager.categories count]; i++) {
                    if([[[sharedManager categories] objectAtIndex:i] isEqualToString:[categoryField text]]) {
                        idx = i;
                        break;
                    }
                }
                [subcategoryField setText:[[[sharedManager subcategories]objectAtIndex:idx] objectAtIndex:row]];
            } @catch (NSException *e) {
                NSLog(@"Exception: %@",e);
               [subcategoryField setText:@"" ];
            }
        }
    }

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

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        WCSharedCache *sharedManager = [WCSharedCache sharedManager];
        if(isCategoryPicker) {
            return [[sharedManager categories]count];
        } else {
            for(int i = 0; i < [sharedManager.categories count]; i++) {
                if([[[sharedManager categories] objectAtIndex:i] isEqualToString:[categoryField text]]) {
                    return [[[sharedManager subcategories] objectAtIndex:i] count];
                }
            }
        }
        return 0;
    }

    - (NSString *)pickerView: (UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component
    {
        WCSharedCache *sharedManager = [WCSharedCache sharedManager];
        if(isCategoryPicker) {
            return [[sharedManager categories] objectAtIndex:row];
        } else {
            for(int i = 0; i < [sharedManager.categories count]; i++) {
                if([[[sharedManager categories] objectAtIndex:i] isEqualToString:[categoryField text]]) {
                    return [[[sharedManager subcategories] objectAtIndex:i] objectAtIndex:row];
                }
            }
        }
        return @"";
    }

Is there a method I can implement, or a try-catch, or revival of original keyboard, or somewhere I can prevent the user from hitting this field if there is no data any of those would work. The data for categories is in an NSArray. The data for subcategories is in a two-dimensional NSArray indexed off of the index of the associated category.


Solution

  • If fixed this problem by changing the default value in:

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
        {
            WCSharedCache *sharedManager = [WCSharedCache sharedManager];
            if(isCategoryPicker) {
                return [[sharedManager categories]count];
            } else {
                for(int i = 0; i < [sharedManager.categories count]; i++) {
                    if([[[sharedManager categories] objectAtIndex:i] isEqualToString:[categoryField text]]) {
                        return [[[sharedManager subcategories] objectAtIndex:i] count];
                    }
                }
            }
            return 1;
        }
    

    from zero to one. Apparently, UIPickerView (at least in the implementation above) cannot handle scrolling without throwing an exception if the number of rows in the component is 0. This is curious, because according the Apple's documentation of the UIPickerView class, the default value for this method is 0.