6

只是为了澄清一下,我不是在问如何在设置 UIpickerview 时一开始就设置行高。我知道您为此目的使用了 pickerView:rowHeightForComponent。

但是,我要问的是我是否希望 pickerView:rowHeightForComponent 返回一个变量值,该值可以在 UIpickerview 的生命周期内更改,例如,响应单击按钮或更改默认设置。

我发现,不幸的是,pickerView:rowHeightForComponent 只被调用一次,一开始,当 iOS 设置 UIpickerview 时。之后,它再也不会调用pickerView:rowHeightForComponent,因此无法获取pickerView:rowHeightForComponent 如果再次调用它会返回的值的变化。

我想我可以释放 UIpickerview 并设置另一个,迫使 iOS 再次调用 pickerView:rowHeightForComponent ,但这可能不方便,并且可能需要保存状态信息。还有另一种更简单的方法来动态更改行高吗?谢谢!

环顾四周,我发现更改 UIPickerView 行高(这只是第一次设置行高)和如何通过按钮单击更改 UIPickerView 中的行大小?,其中讨论了 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] 现在 scrollviewSpacing 是 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 时被调用,并且再也不会被调用。

4

2 回答 2

0

所有委托方法都会在您调用时再次调用并更新[yourPicker reloadAllComponents];

查看UIPickerViewUIPickerViewDelegate文档。尤其是查看“重新加载视图选择器”部分。

于 2012-11-28T17:55:15.983 回答
0

我不确定是否UIPickerView可以动态更改行高。

但我确实实现了一个选择器视图名称DLPickerView。您可以为不同的行返回不同的高度。理论上它还可以在其整个生命周期内改变行高

于 2017-03-22T10:01:43.747 回答