【问题标题】:Several UIPickerView几个 UIPickerView
【发布时间】:2012-03-12 17:03:49
【问题描述】:

我的 viewController 中有几个 UIPickerViews。

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

我使用这些委托方法:

- (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与其他选择器的格式不同?

非常感谢!

【问题讨论】:

  • 有趣...你在第二种方法中为其他选择器返回 nil 吗?
  • 是的,这会是问题吗?如果我不返回任何内容,就会出现警告,所以...我会立即尝试删除该代码。
  • 好吧..不。您不必删除该行,完全可以。
  • 好吧.. 我认为只有两种选择。这两者都不是很简单 1. 使用不同的委托或 2. 创建一个看起来像 UIPicker 使用的 UILabel 并将其发送给其余的选择器,并使用现有的 titleForRow 方法来填写文本值
  • 谢谢!我想使用不同的代表很适合我,但我该怎么办?我的意思是,它们都是 UIPickerView 并且在同一个 viewController 中,我如何区分这两个委托? @govi

标签: ios methods delegates uipickerview


【解决方案1】:

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

@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;
    }
}

当然,你会在现有的课堂上这样做。

【讨论】:

    【解决方案2】:

    我会进一步添加“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) { } }
    

    当你希望你的 Picker 简单地显示一些值时,这是特别使用的。而如果您想为显示的标签添加一些自定义(即宽度,高度,字体......),则使用上述一个..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-15
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多