【发布时间】:2012-11-28 17:20:41
【问题描述】:
只是为了澄清,我不是在问如何在设置 UIpickerview 时一开始就设置行高。我知道您为此目的使用了 pickerView:rowHeightForComponent。
但是,我要问的是是否希望 pickerView:rowHeightForComponent 返回一个变量值,该值可以在 UIpickerview 的生命周期内更改,例如,响应单击按钮或更改默认设置.
我发现,不幸的是,pickerView:rowHeightForComponent 只被调用一次,在开始时,当 iOS 设置 UIpickerview 时。之后,它再也不会调用pickerView:rowHeightForComponent,因此无法获取pickerView:rowHeightForComponent 再次调用时返回的值的变化。
我想我可以只释放 UIpickerview 并设置另一个,迫使 iOS 再次调用 pickerView:rowHeightForComponent,但这可能不方便,并且可能需要保存状态信息。还有另一种更简单的方法来动态更改行高吗?谢谢!
四处搜索,我找到了Changing UIPickerView row height(这只是第一次设置行高)和How to change the size of row in UIPickerView by button click?,它谈到了rowSizeForComponent方法,它似乎只是使用缓存的行高值。
这是尝试更改行高的代码部分,以及测试 reloadAllComponents: 正如@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);
在控制台上给出以下输出:
2012-11-29 02:32:14.681 IP 地址计算器[6404:c07] 之前,scrollviewSpacing 为 39
2012-11-29 02:32:18.913 IP 地址计算器 [6404:c07] 现在滚动视图间距为 22
2012-11-29 02:32:20.196 IP 地址计算器 [6404:c07] 组件高度为 39.000000 2012-11-29 02:32:28.865 IP 地址计算器 [6404:c07] 组件高度为 39.000000 2012-11-29 02:32:30.903 IP 地址计算器 [6404:c07] rowsizeforcomponent 高度为 39.000000
在 MainViewController(即 UIpickerviewdelegate)中,我有
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return self.IACscrollviewSpacing; }
此外,我在这里设置了一个断点,我发现它仅在最初设置 UIPickerview 时才被调用,以后再也不会调用。
【问题讨论】:
-
你是说如果你在选择器视图上调用
reloadComponent:会再次调用rowHeightForComponent:吗? -
我是说 reloadAllComponents: 不会再次调用 rowHeightForComponent: ,这对我来说是个问题。如果是这样,那么它将获取我想用于行高的新值。
-
对不起,我有一个错字。我的意思是“没有被调用”,而不是“被调用”。你现在已经确认了。这是出乎意料的。
标签: ios uipickerview