gpt4 book ai didi

iphone - -[__ NSCFDictionary objectAtIndex:]:将JSON页面解析为UITableView时,无法识别的选择器发送到实例0x8943940

转载 作者:行者123 更新时间:2023-12-01 17:26:19 25 4
gpt4 key购买 nike

我经历了很多链接,幸运的是,我得到了很多类似的链接,但没有解决。

我的数组使用NSLog()出现在Output中。

products =     (
{
cid = 1;
name = "hello world";
},
{
cid = 2;
name = "It is an array";
},
{
cid = 3;
name = "error error";
},
{
cid = 4;
name = "OMG! still same";
},
{
cid = 5;
name = "any help";

...
...
},
{
cid = 130;
name = "How is the work going on.";
},
{
cid = 131;
name = "Had a nice lunch";
}
);
success = 1

解析JSON的代码,其中“新闻”是一个数组变量。
 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
news = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"Feeds...%@",news);
[myTableView reloadData];
}

显示表行的代码,此处为“news”值,NSLog()将显示该行; NSDictionary错误。
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"=====%@====",news);
NSDictionary *myDict = [news objectAtIndex:indexPath.row];
NSArray *myArray = [myDict objectForKey:@"name"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}

for (NSString *str in myArray) {
cell.textLabel.text = str;
}
return cell;

}

我找不到我在这里缺少的东西..,请帮帮我。

先感谢您。

最佳答案

您的JSON结构如下所示:

字典->数组->对象(字典)。

因此,您需要执行以下操作:

  • 使用键product
  • 从字典中提取数组
  • 使用index
  • 从数组中提取一个对象(字典)
  • 使用name键从字典对象中获取名称

  • 代替这个:
    NSDictionary *myDict = [news objectAtIndex:indexPath.row];
    NSArray *myArray = [myDict objectForKey:@"name"];

    用:
    NSArray *myArr = [news objectForKey:@"products"];
    cell.textLabel.text = [[myArr objectAtIndex:0] objectForKey:@"name"];

    编辑

    同样更改您的 numberOfRowsInSection:
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [[news objectForKey:@"products"] count];
    }

    关于iphone - -[__ NSCFDictionary objectAtIndex:]:将JSON页面解析为UITableView时,无法识别的选择器发送到实例0x8943940,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16521692/

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