gpt4 book ai didi

ios - UIPickerView 无法显示 JSON 数组数据

转载 作者:行者123 更新时间:2023-11-28 20:09:50 28 4
gpt4 key购买 nike

这是我第一次做 iOS 应用程序,所以我可能不知道一些看似简单的东西。请耐心等待我 =)

基本上,我需要用我的 JSON 中的数据填充我的 UIPickerView。我已经成功解析了我的 JSON 数据并提取了我需要的内容并将它们放入 NSMutableArray 中。当我第一次尝试做 UIPickerView 时,我使用了硬编码数组,一切正常。现在,当我尝试用我的 JSON 数组替换硬编码数组时,我总是得到一个空 JSON 数组。它的工作方式似乎是 UIPickerView 试图在我的 JSON 数组被填充之前拉出所有内容。

我已尝试按照本网站其他地方的建议重新加载 UIPickerView,但它也不起作用。

以下是我的部分代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

dispatch_async(randomiserBgQ, ^{
NSData* data = [NSData dataWithContentsOfURL:randomiserCatURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}



- (void)fetchedData:(NSData *)responseData{
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSArray* randomiserCat = [json objectForKey:@"categories"];
self.randCatSel =[[NSMutableArray alloc] init];

NSLog(@"categories: %@", randomiserCat);

for (int i = 0; i < [randomiserCat count]; i++) {
NSDictionary *dict = [randomiserCat objectAtIndex:i];
NSString* parent = [dict objectForKey:@"name"];
[self.randCatSel addObject:parent];
NSArray* childrenArray = [dict objectForKey:@"children"];

for (int i = 0; i < [childrenArray count]; i++) {
NSDictionary* dict = [childrenArray objectAtIndex:i];
NSString* children = [dict objectForKey:@"name"];
[self.randCatSel addObject:children];
NSArray* grandchildrenArray = [dict objectForKey:@"grandchildren"];

for (int i = 0; i < [grandchildrenArray count]; i++) {
NSDictionary* dict = [grandchildrenArray objectAtIndex:i];
NSString* grandchildren = [dict objectForKey:@"name"];
[self.randCatSel addObject:grandchildren];
}
}
}
}

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


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
return [self.randCatSel count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.randCatSel objectAtIndex:row];
}

如您所见,我尝试解析 JSON 数据的方式使用了很长的方法,因为我在 JSON 数据中有几层数组。如果有更快的方法(如 PHP 中的 foreach),也请指出正确的方向。

到目前为止,我的 UIPickerView 得到一个空的 randCatSel 数组(我假设它在填充之前加载它)。

最佳答案

你好,请试试这个,

- (void)fetchedData:(NSData *)responseData
{
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSArray* randomiserCat = [json objectForKey:@"categories"];
self.randCatSel =[[NSMutableArray alloc] init];

NSLog(@"categories: %@", randomiserCat);

for (int i = 0; i < [randomiserCat count]; i++) {
NSDictionary *dict = [randomiserCat objectAtIndex:i];
NSString* parent = [dict objectForKey:@"name"];
[self.randCatSel addObject:parent];
NSArray* childrenArray = [dict objectForKey:@"children"];

for (int i = 0; i < [childrenArray count]; i++) {
NSDictionary* dict = [childrenArray objectAtIndex:i];
NSString* children = [dict objectForKey:@"name"];
[self.randCatSel addObject:children];
NSArray* grandchildrenArray = [dict objectForKey:@"grandchildren"];

for (int i = 0; i < [grandchildrenArray count]; i++) {
NSDictionary* dict = [grandchildrenArray objectAtIndex:i];
NSString* grandchildren = [dict objectForKey:@"name"];
[self.randCatSel addObject:grandchildren];
}
}
}
packerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
packerView.delegate=self;
packerView.dataSource=self;
[self.view addSubview:packerView];

}
//add the picker view delegate and datasource in .h file. and set the frame as per your requirement.

希望对您有所帮助。

关于ios - UIPickerView 无法显示 JSON 数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20737145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com