两个 UIPickerView 对象可以共享同一个数据源数组吗?
我在 Xcode 4.02 中使用 Interface Builder 在视图中添加两个 UIPickerView。它们连接到相同的委托和数据源(UIViewController)。在我的 .h 文件中,我还声明了 UIPickerViews 并将它们连接为参考出口,如许多示例所示。 在 viewForRow 中,我使用 UILabels 的相同 NSMutable 数据数组来返回适当的值(该数组在 viewDidLoad 中填充)。
但是,我发现两个 UIPickerView 中没有同时出现 Label。例如,当应用程序启动时,每个 UIPickerView 应显示元素 0、1 和 2。每个 UIPickerView 都会调用 viewForRow 3 次,但只有第二次调用 viewForRow 的视图才会显示前 3 行。另一个 UIPickerView 是空白的。如果我将第一个视图向下滚动到第 6 个元素,然后返回到第一个,则该视图将显示前 3 个元素,但第二个视图(显示前 3 行的那个)现在什么也不显示。具体来说,不会有数据数组元素同时出现在两个 UIPickerView 中。
这是预期的吗?每个 UIPickerView 是否应该有自己的支持数组 - 如果您正在使用它们?所有这些返回的视图不都是指针吗?就好像每个数组元素在任何时候最多只能显示(指向)一次。
如果我使用两个单独的数据数组,那么似乎没有问题。但这确实意味着额外的内存和额外的编码。
注意:在 viewForRow 中,我有代码来设置标签的大小:
UILabel *xx = (UILabel *)[self.array1 objectAtIndex:row];
CGSize rowSize = [thePickerView rowSizeForComponent:component];
CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
[xx setFrame:labelRect];
即使该行稍后显示为空白,此处设置的值也不会更改。
I have used Interface Builder in Xcode 4.02 to add two UIPickerViews in a View. They are connected to the same delegate and datasource (UIViewController). In my .h file I have also declared UIPickerViews and connected them as reference outlets, as in many examples.
In viewForRow I use the same NSMutable data array of UILabels to return the appropriate values (this array was populated in viewDidLoad).
However, I find that no Label appears in both UIPickerViews at the same time. For example, when the app starts each UIPickerView should show elements 0, 1 and 2. viewForRow is invoked 3 times for each UIPickerView but only the view which invokes viewForRow second will display the first 3 rows. The other UIPickerView is blank. If I scroll the first view down to, say, the 6th element and then back to the first, the view will then show the first 3 elements but the second view (the one that did show the first 3 rows) now shows nothing. Specifically, no data array element will appear in both UIPickerViews at the same time.
Is this expected? Should each UIPickerView have its own backing array - if you're using them? Aren't all these returned views just pointers? It's as if each array element can only be displayed (pointed to) at most once at any time.
If I use two separate data arrays then there appear to be no problems. But it does mean extra memory and extra coding.
Note: in viewForRow I have code to set the label's size:
UILabel *xx = (UILabel *)[self.array1 objectAtIndex:row];
CGSize rowSize = [thePickerView rowSizeForComponent:component];
CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
[xx setFrame:labelRect];
The values set here do not change even when the row later appears to be blank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多个选取器视图可以共享相同的数据源,但一个视图(本例中为
UILabel
)只能有一个超级视图。您不能在两个选择器中使用相同的标签,并且可能没有什么理由将它们存储在您自己的数组中。更好的方法是在 viewForRow 方法中创建一个单独的标签,并只有一个包含标签内容的数组(例如 NSString )。
在当前的实现中,当您返回标签时,选择器会将其添加到自己的视图层次结构中,这会隐式地将其从之前所在的任何视图(您的其他选择器)中删除。
Multiple picker views can share the same data source but a view (
UILabel
in this case) can only have one superview.You cannot use the same labels in both pickers and there's probably little reason to store them in an array of your own anyway. A better approach would be to create a separate label in the
viewForRow
method and have just an array with the labels' content (e.g. anNSString
).In your current implementation, when you return the label, the picker adds it to its own view hierarchy which implicitly removes it from any view it was in before (your other picker).