Question

I have a modal view that has two pickers, a date picker and a two component picker with a Category and Subcategory for content

I dynamically set the contents of the category/subcategory picker depending on what is chosen in the first component to drive the content of the second component.

When I load the modal view as a new modal view and have everything set to defaults it works fine. Today's date shows up, and rows 0 of the category/subcategory picker shows up. If I change the values and exit the modal view, all is well and I store the values from the pickers correctly in the parent view.

My challenge is when I load up the modal view with specific values to reflect a selection. Say I had a transaction done February 18, 2014, and the category is Transportation, sub category is Train.

The date picker shows the correct value, but the category/subcategory shows the incorrect value if the choice is the last entry in an array.

Let me illustrate:

Say my categories are: Dining, Entertainment, Groceries, Misc, Transportation

For Subcategories of Transportation: Bus, Gas, Subway, Taxi, Train

If I initialize the picker with Entertainment and Music it looks fine, but if I initialize it with Tranportation and Train, the picker shows Miscellaneous and Taxi because Transportation and Train are the last elements of the array. If I initialize it with Transportation and Gas, it shows up as Miscellaneous and Gas. As you can see Transportation always appears as Miscallaneous because Transportation is the last entry in an array.

enter image description here enter image description here

To simplify my question, I simplified the code as such:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (self.dateCreated) {
        [self.datePicker setDate:self.dateCreated];
    }

    self.categoryArray  = [[NSArray alloc]         initWithObjects:@"Dining",@"Entertainment",@"Groceries",@"Miscellaneious",@"Transportation",nil];
    self.subCategoryArray  = [[NSArray alloc]         initWithObjects:@"Bus",@"Gas",@"Subway",@"Taxi",@"Train", nil];

    self.selectedCategoryRow = 4;
    self.selectedSubCategoryRow = 4;

    [self.categoryPicker selectRow:4 inComponent:0 animated:YES];
    [self.categoryPicker selectRow:4 inComponent:1 animated:YES];
    [self.categoryPicker reloadAllComponents];  // NOT REALLY NEEDED
}

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    int num = 0;
    if (0 == component) {
        num = [self.categoryArray count];
    } else if (1 == component) {
        num = [self.subCategoryArray count];
    } else {
        NSLog(@"Unknown component %d", component);
    }
    return num;
}

The only thing I found that fixed it is if in the storyboard of the picker I uncheck AutoresizeSubviews. At that point, the picker shows the correct value, but it looks mangled as such:

enter image description here

Is there something I can do in the code to work around this?

For reference, here is how things are configured for the picker in the storyboard:

enter image description here enter image description here

So to fix the issue, I added this code in View did load:

self.categoryPicker.frame = CGRectMake(20, 332, 280, 216);

Changing the setting of the autoresize was not needed in the storyboard so I restored it.

Let me know if this is a hack or not.

Was it helpful?

Solution 2

To fix the issue, I added this code in View did load :

self.categoryPicker.frame = CGRectMake(20, 332, 280, 216);

Changing the setting of the autoresize was not needed in the storyboard so I restored it.

OTHER TIPS

You should initialize your selected rows after calling reloadAllComponents.

[self.categoryPicker reloadAllComponents];
[self.categoryPicker selectRow:4 inComponent:0 animated:YES];
[self.categoryPicker selectRow:4 inComponent:1 animated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top