【问题标题】:How set UIPickerView in cascade如何在级联中设置 UIPickerView
【发布时间】:2014-03-20 15:41:34
【问题描述】:

我正在尝试设置两个UIPickerViews,两个都填充NSDictionary,这个工作正常,但我需要第二个UIPickerView实现刷新取决于第一个UIPickerView的选择,懂我吗?

第一个UIPickerView是这样填充的:

for (NSDictionary *local in json) {
    NSString *nombre = [local objectForKey:@"name"];
    NSString *idLocal = [local objectForKey:@"id"];

    self.dicLocales = [NSDictionary dictionaryWithObjectsAndKeys: idLocal, @"idLocal", nombre, @"nombreLocal", nil];
    [listaLocales addObject:self.dicLocales];
}

第二个UIPickerView是这样填充的:

for (NSDictionary *servicio in json) {
    NSString *nombre = [servicio objectForKey:@"name"];
    NSString *idServicio = [servicio objectForKey:@"id"];

    self.dicServicios = [NSDictionary dictionaryWithObjectsAndKeys: idServicio, @"idServicio", nombre, @"nombreServicio", idLocal, @"idLocal", nil];
    [listaServicios1 addObject:self.dicServicios];
}

您注意到变量idLocal,它对应于第一行UIPickerView 的id。然后,我需要,如果在第一个 UIPickerView 中选择 idLocal 1 的行,则在第二个 UIPickerView 中重新加载或刷新仅与 idLocal 1 对应的行。

希望有人可以帮助我,对不起我的英语:P

【问题讨论】:

    标签: ios view uipickerview picker


    【解决方案1】:

    你必须实现<UIPickerViewDelegate>方法如下

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        if ([pickerView isEqual:FirstPickerView]) {
            //SecondPickerView fill code
            [SecondPickerView reloadAllComponents];
        }
        else {
            //SecondPickerView Selected row
        }
    }
    

    并相应地更改您的第二个 UIPickerView 填充循环

    【讨论】:

      【解决方案2】:

      我建议使用 (NSInteger) 组件 0 是第一行,依此类推。

      if (component == 0) {
          if (row == 0) {
      
          }
          else if (row == 1)
          {
      
          }
      }
      else if (component == 1)
      {
          selectedPositionIndex = row;
          if (row == 0) {
      
          }
          else if (row == 1)
          {
      
          }
          else if (row == 2)
          {
      
          }
          else if (row == 3)
          {
      
          }
      }
      

      此代码适用于几乎所有提供组件 NSInteger 的选取器视图方法。

      -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
      
      
      - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
      

      您需要确保为每个组件返回正确的项目数

      -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
      {
      if (component == 0) {
          return wordArray.count;
      }
      else if (component == 1)
      {
          return positionArray.count;
      }
      return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-21
        • 1970-01-01
        • 2012-07-31
        • 2012-11-25
        • 1970-01-01
        相关资源
        最近更新 更多