【问题标题】:passing Json array to UIPickerView Not working将 Json 数组传递给 UIPickerView 不起作用
【发布时间】:2014-07-22 05:27:29
【问题描述】:

我从 JSON 获取数据。像这样

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

   al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    NSLog(@"String String %@",al);

    for (array in al) {
        NSLog(@"array is  array %@",array);
    }
}

PickerView 方法:-

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
        return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
      numberOfRowsInComponent:(NSInteger)component
{
        return array.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
    return array[row];
}

我试过这样,但它显示的是空的 PickerView。所以请给我任何想法。

我的 Json 是这样的:

[[1,"Hyd"],[2,"viz"]]

【问题讨论】:

  • 您正在接收二维数组,请确保您像二维数组一样解析它。
  • 获取数据后尝试重新加载选择器视图。
  • 为了使您的代码正常工作,您需要 JSON 如下所示:["Hyd","viz"]
  • @rmaddy 感谢重播我尝试重新加载但无法正常工作 [thePicker reloadAllComponents];
  • @SylvainGuillopé 感谢您的回复,当我在浏览器上点击 url 时,我需要将 json 数据发送到 UIPickerView,它显示如下 [[1,"Hyd"],[2,"viz"]] 我试过了但没有运气所以请给我任何想法

标签: objective-c json nsarray uipickerview


【解决方案1】:

这样的事情应该可以工作:

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
      return [((NSArray *)array[0]) count]; // check for boundary conditions
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
      numberOfRowsInComponent:(NSInteger)component
{
     return array.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
    return array[row][1]; // title exists at 1 index of each 2d array.
}

【讨论】:

  • 感谢您所说的重播,但pickerview仍然为空请给我任何想法
  • 确保通过 IB 或代码连接 delegatedatasource。这段代码对我有用。
猜你喜欢
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多