如何在同一类中为不同的选择器使用 viewForRow 和 titleForRow?

发布于 2024-10-03 14:43:33 字数 1115 浏览 5 评论 0原文

我的视图中有 3 个选择器,其中之一仅包含图像。

因此,对于这一个,我必须使用 viewForRow,但对于其他 2 个(带有标签的常见选择器),我需要 titleForRow,但如果我使用 viewForRow,则 titleForRow 永远不会被调用。

我怎样才能在我的班级中召集两名代表?

这就是我所拥有的:

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

    [[[objectPicker subviews] objectAtIndex:0] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:5] setHidden:YES]; 
    [[[objectPicker subviews] objectAtIndex:8] setHidden:YES];

    return ; //will be implemented, but this will return the icons
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    [[[objectPicker subviews] objectAtIndex:0] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:5] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:8] setHidden:YES]; 

    if (pickerView == self.timePicker) {
        return [timeList objectAtIndex:row];
    }
    else if(pickerView == self.powerPicker) {
        return [powerList objectAtIndex:row];
    }
}

I have 3 pickers on my view, one of which contains only images.

So, for this one, I have to use viewForRow, but for the other 2, which are common pickers with labels, I need titleForRow, except that if I use viewForRow, titleForRow never gets called.

How can I have both delegates called in my class?

This is what I have:

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

    [[[objectPicker subviews] objectAtIndex:0] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:5] setHidden:YES]; 
    [[[objectPicker subviews] objectAtIndex:8] setHidden:YES];

    return ; //will be implemented, but this will return the icons
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    [[[objectPicker subviews] objectAtIndex:0] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:5] setHidden:YES];
    [[[objectPicker subviews] objectAtIndex:8] setHidden:YES]; 

    if (pickerView == self.timePicker) {
        return [timeList objectAtIndex:row];
    }
    else if(pickerView == self.powerPicker) {
        return [powerList objectAtIndex:row];
    }
}

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

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

发布评论

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

评论(2

染柒℉ 2024-10-10 14:43:33

您根本不能将两者放在同一个委托中。解决方案是仅实现 viewForRow: 并返回文本列的 UILabel 对象。将 label.text 设置为您在 textForRow: 中返回的字符串。

You simply can't have both in the same delegate. The solution is to only implement viewForRow: and return a UILabel object for the text columns. Set label.text to the string you're returning in textForRow:.

陌路黄昏 2024-10-10 14:43:33

为每个选择器创建不同的类,每个类都包含自己的委托。
然后简单地将它们导入到我的 mainController 类中并在 viewDidLoad 方法中初始化它们。

这是我使用的解决方案

L =============================找到解决方案(解决方法):========== =================

首先,我为 3 个选择器创建了 3 个不同的类,每个类都有不同的 .xib。将这 3 个选择器添加到我的主控制器类中:

//不要忘记 #import "YourClass.h" 并在主控制器的 .h 文件上实例化它

objPickerVC = [[ObjectPickerViewController alloc] initWithNibName:@"ObjectPickerViewController" bundle:[NSBundle mainBundle]];
powerPickerVC = [[PowerPickerViewController alloc] initWithNibName:@"PowerPickerViewController" bundle:[NSBundle mainBundle]];
timePickerVC = [[TimePickerViewController alloc] initWithNibName:@"TimePickerViewController" bundle:[NSBundle mainBundle]];

CGRect rect1 = CGRectMake(5, 43, 100, 216); 
objPickerVC.view.frame = rect1;
[self.view addSubview:objPickerVC.view];

CGRect rect2 = CGRectMake(110, 43, 100, 216); 
powerPickerVC.view.frame = rect2;
[self.view addSubview:powerPickerVC.view];

CGRect rect3 = CGRectMake(215, 43, 100, 216);
timePickerVC.view.frame = rect3;
[self.view addSubview:timePickerVC.view];

通过这样做,我为每个 pickerView 都有单独的委托,允许我设置但我需要它们。

PS:对于定位问题,重要的是在你的 pickerViews xib 上将它们集中在视图上。如果不这样做,当您尝试放置它们时 ->例如,CGRectMake(215, 43...) 不会位于 REAL (215, 43...) 位置。我知道,这没有任何意义,但这是一个可怕的错误,让你必须将其放置在试验中。改变值-运行-改变-运行,直到你得到正确的结果。

Created different classes for each of the pickers, each one containing their own delegates.
Then simply imported them into my mainController class and initialized them in the viewDidLoad method.

Here is the solution I usedL

===========================Found the solution(workaround):===========================

First, I created 3 different classes for my 3 pickers, each one with a different .xib. Added these 3 pickers into my main controller class:

//Don't forget #import "YourClass.h" and instantiate it on the .h file of your main controller

objPickerVC = [[ObjectPickerViewController alloc] initWithNibName:@"ObjectPickerViewController" bundle:[NSBundle mainBundle]];
powerPickerVC = [[PowerPickerViewController alloc] initWithNibName:@"PowerPickerViewController" bundle:[NSBundle mainBundle]];
timePickerVC = [[TimePickerViewController alloc] initWithNibName:@"TimePickerViewController" bundle:[NSBundle mainBundle]];

CGRect rect1 = CGRectMake(5, 43, 100, 216); 
objPickerVC.view.frame = rect1;
[self.view addSubview:objPickerVC.view];

CGRect rect2 = CGRectMake(110, 43, 100, 216); 
powerPickerVC.view.frame = rect2;
[self.view addSubview:powerPickerVC.view];

CGRect rect3 = CGRectMake(215, 43, 100, 216);
timePickerVC.view.frame = rect3;
[self.view addSubview:timePickerVC.view];

By doing that, I have individual delegates for each pickerView allowing me to set them however I need them to be.

P.S: It's important, for positioning matters, that on your pickerViews xib you centralize 'em on the view. If you don't, when you try to position them -> CGRectMake(215, 43...) for example, will not be on the REAL (215, 43...) position. I know, it doesn't make any sense, but it's an awful bug and makes you have to position it on trials. Change value - run - change - run, until you get it right.

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