代码之家  ›  专栏  ›  技术社区  ›  Prody

Cocoa Touch:UIPickerView视图ForRow正在更改行顺序

  •  0
  • Prody  · 技术社区  · 16 年前

    我有一个UIPickerView。我正在通过其代理人的viewForRow自定义其行,如下所示:

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
        if (view) {
            return view;
        } else {
            NSString *s = [datePickerValues objectAtIndex:row];
    
            UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)] autorelease];
    //        l.text = s;
            l.text = [NSString stringWithFormat:@"%@ r:%ld", s, row];
            l.font = [UIFont boldSystemFontOfSize:18];
            l.textAlignment = UITextAlignmentCenter;
            l.backgroundColor = [UIColor purpleColor];
            return l;
        }
    }
    

    当我旋转它一段时间时,行就会混淆。
    viewForRow 方法有误 row

    component param总是 0 ,我已经用调试器验证了这一点。

    另一件奇怪的事情是,根据文档,一旦我为特定行创建了视图,代理人的 view viewForRow 有时会对同一行多次调用 view = nil .

    知道为什么会有这种奇怪的行为吗?我刚接触可可和Obj-C,我做错什么了吗?

    编辑:


    viewForRow
    viewForRow 每当显示一行时,即使该行之前已经显示过,也会被调用。

    3 回复  |  直到 16 年前
        1
  •  2
  •   zaph    16 年前

    每次调用此方法时,您都必须更新视图中的数据。不更新会返回过时/重复的数据。

        2
  •  1
  •   Vladimir    16 年前

    关于 (view != nil) 如果你的视图始终是UILabel,你可以这样重写代码(抱歉没有编译它):

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
        NSString *s = [datePickerValues objectAtIndex:row];
    
        UILabel *l = (view != nil)? view : [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)] autorelease];
        l.text = [NSString stringWithFormat:@"%@ r:%ld", s, row];
        l.font = [UIFont boldSystemFontOfSize:18];
        l.textAlignment = UITextAlignmentCenter;
        l.backgroundColor = [UIColor purpleColor];
        return l;
    }
    

    它必须工作正常。

        3
  •  0
  •   fadecutmike    12 年前

    我看到这是一个非常古老的问题,但我在iOS7上也遇到了同样的问题,找不到令人满意的答案。当我试图从viewForRow委托方法中访问“row”参数时,它会返回不正确和混淆的值。

    我通过以下方式解决了这个问题,

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    ...
    
        //Create a label
        //Set a tag for label
        //Add label to view as a subview
        //return view
    
     if (component == 1) {
        view = [[UIView alloc] init];
        view.backgroundColor = [UIColor clearColor];
        correctRowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
        [correctRowLabel setText:[NSString stringWithFormat:@"%li", row]];
        [correctRowLabel setTag:100];
        [view addSubview: correctRowLabel];
        return view;
      }
    

    根据需要设置组件后,在viewForRow委托方法的开头,执行以下操作以查看正确的行值,

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
        UILabel* correctRow = (UILabel*)[pickerView viewWithTag:100];
        NSInteger correctRowValue = [correctRow.text integerValue];
        NSLog(@"Correct row is: %li",correctRowValue);
        UILabel* correctRowLabel = nil;
    
    ...
    

    编辑: