几个UIPickerView

发布于 2025-01-08 08:07:47 字数 980 浏览 7 评论 0原文

我的 viewController 中有几个 UIPickerView。

这是其中之一需要自定义,因为它在 1 个选择器行中显示 2 个 UILabels。

我使用这些委托方法:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component {
    // this method is use for normal pickers, and I would judge whether the picker that calling
    // this method is a normal one or the special one by pickerView.
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
    // and this one is specially for the special picker, I would also judge the pickerView is  
    //normal or special, if pickerView is normal, return nil, else I return a UIView with 
    //2 UIlabels.
}

但是现在经过调试,我发现如果我一起实现这两个方法,第二个方法总是被调用,而第一个方法似乎永远不会被调用,

这导致我的特殊选择器显示正确数据,但其他人什么都没有。

我怎么能这样做呢?

如果我在第二种方法中给出所有选择器的数据,那么 reusingView 是否会成为问题,因为特殊选择器的 reusingView 与其他选择器的格式不同?

多谢!

I have several UIPickerViews in my viewController.

And this is one of them need to be customized since it displays 2 UILabels in 1 picker row.

And I use these delegate methods:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component {
    // this method is use for normal pickers, and I would judge whether the picker that calling
    // this method is a normal one or the special one by pickerView.
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
    // and this one is specially for the special picker, I would also judge the pickerView is  
    //normal or special, if pickerView is normal, return nil, else I return a UIView with 
    //2 UIlabels.
}

But now after my debugging, I found that if I implement the 2 methods together, the second one is always called and the first one seems never to be called,

and it results in that my special picker displays correct data, but others have nothing.

How could I do this?

if I give all the pickers' data in the second method, would the reusingView become a problem, since the special picker's reusingView is not in the same format as other picker?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走干脆点 2025-01-15 08:07:47

但使用不同的委托并不是那么简单。创建一个单独的类,

@interface MyCustomDelegate : NSObject <UIPickerViewDelegate> 

并实现委托方法,在这种情况下

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component {
    // this method is use for normal pickers, and I would judge whether the picker that calling
    // this method is a normal one or the special one by pickerView.
}

,一旦执行此操作,创建此类的实例并将其设置为委托,例如......

pickerView.delegate = [[MyCustomDelegate alloc] initWithData:data] autorelease];

其中数据可能是您可能想要包含在标题的委托方法。

您可以为所有选择器使用相同的委托实例,也可以创建单独的实例。这在很大程度上取决于您拥有的数据类型。如果它们彼此无关,那么最好使用单独的实例,否则您可以只使用一个实例。

嗯,就是这样。


至于第二个,
你会做这样的事情

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    if (pickerView==specialPickerView) {

    }
    else {
        UILabel *lbl = [[UILabel alloc] init];
        lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        //do some more styling like setting up the font as bold
        //adding some padding to the text and some shiny things
        return lbl;
    }
}

当然,你会在现有的班级中这样做。

Using a different delegate isnt that straightforward though. Create a separate class,

@interface MyCustomDelegate : NSObject <UIPickerViewDelegate> 

and implement the delegate methods, in this case

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component {
    // this method is use for normal pickers, and I would judge whether the picker that calling
    // this method is a normal one or the special one by pickerView.
}

Once you do this, create an instance of this class and set it as the delegate like..

pickerView.delegate = [[MyCustomDelegate alloc] initWithData:data] autorelease];

where the data might be the data you might want to include to use in the delegate methods for the titles.

You can use the same delegate instance for all the pickers, or can create separate instances. The depends a lot on the kind of data you have. If they are all unrelated to each other, it would be nice to use separate instances, else you could go ahead with just the one.

Well that's that.


As for the second one,
you would be doing something like this

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    if (pickerView==specialPickerView) {

    }
    else {
        UILabel *lbl = [[UILabel alloc] init];
        lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        //do some more styling like setting up the font as bold
        //adding some padding to the text and some shiny things
        return lbl;
    }
}

Ofcourse, you would be doing this in your existing class.

短暂陪伴 2025-01-15 08:07:47

我将进一步补充“govi”的上述回复;
“tag”属性(为此,您必须为每个 UIPicker 视图分配不同的“tag”值,即 1、2、3...)也可用于标识多个 UIPikeView; ,

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView.tag ==0) {

}
else if (pickerView.tag ==1) {
    UILabel *lbl = [[UILabel alloc] init];
    lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    //do some more styling like setting up the font as bold
    //adding some padding to the text and some shiny things
    return lbl;
}

例如

您还可以使用其中任何一个

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (pickerView.tag ==0) {    }
else if (pickerView.tag ==1) { } }

。当您希望选择器简单地显示某些值时,特别会使用此选项。而如果您想向显示的标签添加一些自定义(即宽度、高度、字体...),则使用上面的方法。

I would further add to ABOVE response by 'govi';
'tag' property (for this you have to assign different 'tag' values to each UIPicker view, i.e. 1,2,3...) can also be used to identify multiple UIPikeViews; e.g.,

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView.tag ==0) {

}
else if (pickerView.tag ==1) {
    UILabel *lbl = [[UILabel alloc] init];
    lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    //do some more styling like setting up the font as bold
    //adding some padding to the text and some shiny things
    return lbl;
}

}

Further more you can use any of these

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (pickerView.tag ==0) {    }
else if (pickerView.tag ==1) { } }

This is one especially used when u want you Picker to simply display some value. whereas the above one is used if you want to add some Customization to the displayed Label (i.e Width, heigth, Font...)..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文