6

Just to clarify, I am not asking how to set the row height once in the beginning when the UIpickerview is being set up. I know you use pickerView:rowHeightForComponent for that purpose.

However, what I am asking is if I want pickerView:rowHeightForComponent to return a variable value, where the value can change during the lifetime of the UIpickerview, e.g., in response to the click of a button or change in a default setting.

I found that, unfortunately, pickerView:rowHeightForComponent is only called once, in the beginning, when iOS is setting up the UIpickerview. After that, it never calls pickerView:rowHeightForComponent again, so can't pick up the change in the value that pickerView:rowHeightForComponent would return if only it were called again.

I suppose I could just release the UIpickerview and set up another one, forcing iOS to call pickerView:rowHeightForComponent again, but this might be inconvenient, and might need saving of state information. Is there another, easier way, to dynamically change the row height? Thanks!

Searching around, I found Changing UIPickerView row height (which is only for the first time setup of the row height) and How to change the size of row in UIPickerView by button click? , which talks about the rowSizeForComponent method, which appears to just use a cached value for the row height.

Here's the portion of code that attempts to change the row height, together with testing reloadAllComponents: as suggested by @RileyE

NSLog(@"before, scrollviewSpacing is %d", self.mainViewController.IACscrollviewSpacing);
if (0.0 != fromDefaultsScrollviewSpacing) {
    self.mainViewController.IACscrollviewSpacing = (UInt8) round (60.0 - fromDefaultsScrollviewSpacing);
}
NSLog(@"now scrollviewSpacing is %d", self.mainViewController.IACscrollviewSpacing);
NSLog(@"rowsizeforcomponent height is %f", [self.mainViewController.PickIPAddress rowSizeForComponent:1].height);
[self.mainViewController.PickIPAddress reloadAllComponents];
NSLog(@"rowsizeforcomponent height is %f", [self.mainViewController.PickIPAddress rowSizeForComponent:1].height);
[self.mainViewController.PickIPAddress reloadComponent:1];
NSLog(@"rowsizeforcomponent height is %f", [self.mainViewController.PickIPAddress rowSizeForComponent:1].height);

which gives the following output on the console:

2012-11-29 02:32:14.681 IP Address Calculator[6404:c07] before, scrollviewSpacing is 39
2012-11-29 02:32:18.913 IP Address Calculator[6404:c07] now scrollviewSpacing is 22
2012-11-29 02:32:20.196 IP Address Calculator[6404:c07] rowsizeforcomponent height is 39.000000 2012-11-29 02:32:28.865 IP Address Calculator[6404:c07] rowsizeforcomponent height is 39.000000 2012-11-29 02:32:30.903 IP Address Calculator[6404:c07] rowsizeforcomponent height is 39.000000

and in MainViewController (which is the UIpickerviewdelegate), I have

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return self.IACscrollviewSpacing; }

Furthermore, I set a breakpoint here, and I see it only gets called when the UIPickerview is initially being set up, and never again.

Community
  • 1
  • 1
auspicious99
  • 3,902
  • 1
  • 44
  • 58
  • Are you saying that `rowHeightForComponent:` is called again if you call `reloadComponent:` on the picker view? – rmaddy Nov 28 '12 at 18:04
  • I'm saying that reloadAllComponents: doesn't call rowHeightForComponent: again, which is a problem for me. If it does, then it would pick up the new value I want to use for the row height. – auspicious99 Nov 28 '12 at 18:08
  • Sorry, I had a typo. I meant "isn't called", not "is called". You've confirmed that now. This is unexpected. – rmaddy Nov 28 '12 at 18:12

2 Answers2

0

All delegate methods are called again and updated when you call [yourPicker reloadAllComponents];

Check out the UIPickerView and UIPickerViewDelegate docs. Check out the "Reloading the View Picker" section, especially.

RileyE
  • 10,874
  • 13
  • 63
  • 106
  • Read the question again. It's not about table views. – rmaddy Nov 28 '12 at 18:03
  • Thanks, but it is a UIpickerview, not a UItableview. There is no reloadData method. There is reloadAllComponents or reloadComponent: but these only read the data again (UIpickerviewdatasource protocol methods), and don't call the UIpickerviewDelegate protocols methods (at least, not pickerView:rowHeightForComponent). – auspicious99 Nov 28 '12 at 18:04
  • @rmaddy Ah. My bad, however, its the same thing. I've updated my answer. – RileyE Nov 28 '12 at 18:10
  • I know that this works for me. If it doesn't work for you to change the row height, then something is wrong in what you're calling. – RileyE Nov 28 '12 at 18:12
  • @RileyE I just did my own test and when I call either `reloadAllComponents` or `reloadComponent:` on a picker view, all of the appropriate delegate/data source methods are called except for the `pickerView:rowHeightForComponent:` method. I tested under iOS 6. Are you sure that the `rowHeightForComponent:` is called for you after the picker is fully displayed and later reloaded? – rmaddy Nov 28 '12 at 18:29
  • @RileyE It changes the row height for you? Will update the question with code, etc. – auspicious99 Nov 28 '12 at 18:35
  • @rmaddy I had a picker in a previous project with dynamic content and when the content changed, calling reloadAllComponents would redefine the row height. I built it on iOS 4.3, though. Maybe it doesn't work now? – RileyE Nov 28 '12 at 21:31
0

I am not sure if UIPickerView can dynamically change the row height.

But I did implement a picker view name DLPickerView. You can return different height for different row. Theoretically it can also change the row height throughout its lifetime

Dan Lee
  • 101
  • 1
  • 5