将Json数组传递给UIPickerView不起作用

时间:2014-07-22 05:27:29

标签: objective-c json nsarray uipickerview

我从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.So请给我任何想法。

感谢高级。 我的Json喜欢这样: -

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

请告诉我有关我的问题的任何想法。

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

- (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.
}