Why isn't the UIPickerView widthForComponent d

2019-03-01 13:05发布

问题:

I've got two controls on an iPhone screen - a TableView and a UIPickerView. When you select the single cell in the TableView you're taken to another screen where you're show a list of clothing types. Selecting a single clothing item from the list takes you back to the first screen. Based on your selection, the number of components in the UIPickerView is changed (1-4) and each component is populated with relevant data.

Let's say you have three items in you clothing type list and each item should have a certain number of components in the UIPickerView when it is selected:

Belt - 3 components with widths of 100, 55, 100 Brassiere - 3 components with widths of 55, 55, 55 Other - 4 components with widths of 100, 100, 55, 50

Scenario #1 - Everything is as it should be

1.1 - You select the "Belt" clothing item and the UIPickerView is drawn with 3 components with the appropriate Belt data in each component. The component widths are 100, 55, 100.

1.2 - You select the "Other" clothing item and the UIPickerView is drawn with 4 components with the appropriate Other data in each component. The component widths are 100, 100, 55, 50.

1.3 - You select the "Brassiere" clothing item and the UIPickerView is drawn with 3 components with the appropriate Brassiere data in each component. The component widths are 55, 55, 55.

Scenario #2 - Component widths not redrawn

1.1 - You select the "Belt" clothing item and the UIPickerView is drawn with 3 components with the appropriate Belt data in each component. The component widths are 100, 55, 100.

1.2 - You select the "Brassiere" clothing item and the UIPickerView is drawn with 3 components with the appropriate Brassiere data in each component. However, the component widths are still 100, 55, 100, not 55, 55, 55.

1.3 - You select the "Other" clothing item and the UIPickerView is drawn with 4 components with the appropriate Other data in each component. The component widths are 100, 100, 55, 50.

1.2 - You select the "Brassiere" clothing item and the UIPickerView is drawn with 3 components with the appropriate Brassiere data in each component. The component widths are now correct - 55, 55, 55.

From stepping through the code it looks like the UIPickerView widthForComponent delegate method is only called when the number of components in the UIPickerView changes.

Is there any way to force this delegate method to fire every time?

Update - 10.26.09

I am calling [UIPickerView reloadAllComponents in the viewWillAppear delegate using the following code:

-(void)viewWillAppear:(BOOL)animated {

 ...

 self.PopulatePickerArrays; // Populate components based on selection
 [sizePicker reloadAllComponents];

 ...

}

Thanks in advance.

回答1:

It turns out that if you reset the picker's delegate, the component widths are redrawn each time a selection is made. The modified code looks like this:

-(void)viewWillAppear:(BOOL)animated {

 ....

 self.PopulatePickerArray; // Populate components based on selection

 sizePicker.delegate = nil; // NEW CODE
 sizePicker.delegate = self; // NEW CODE

 [picker reloadAllComponents];

}

Found info on this web page - Changing-Width-UIPicker-Continually



回答2:

Actually, just using setNeedsLayout will also work and might be a bit cleaner.

[picker reloadAllComponents];
[picker setNeedsLayout];


回答3:

I am not sure, but you can try to call [pickerView reloadAllComponents]; in your viewWillAppear method.

  if(currentNumbersOfComponents < 4){
    currentNumbersOfComponents++;
    [picker reloadAllComponents];
}

The numberOdComponents method returns currentNumberOfComponents