【问题标题】:How to load JSON values into tableview array using objective C如何使用目标 C 将 JSON 值加载到 tableview 数组中
【发布时间】:2015-07-24 04:32:31
【问题描述】:

如何将jsonDictionary[@"response"][obj][@"color"] 加载到表视图数组中。下面是我正在使用的示例代码,但每次我在return [tableviewArray count]; 遇到严重异常时。请为我的问题提供一些解决方案。

NSError *error;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

// get keys from response dictionary
NSMutableArray * key = [[NSMutableArray alloc] initWithArray:[jsonDictionary[@"response"] allKeys]];

// sort as asending order
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
key =  (NSMutableArray *)[key sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

// access inner data from dictonary
for (NSString * obj in key) {

    // Here below values I want to apply for tableview array
    NSLog(@"%@",jsonDictionary[@"response"][obj][@"color"]);
    NSArray *tableviewArray = [jsonDictionary[@"response"][obj][@"color"]]; 
}

我在下面的 tableview 方法上遇到了严重的异常

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableviewArray count]; // Here am getting bad exception
}

我的 JSON:

{
    response: {
        RED: {
            Color: "red",
            color_id: "01"},
        GREEN: {
            Color: "green",
            color_id: "02"}
    },
    Colorcode: {},
    totalcolor: "122"
}

【问题讨论】:

  • 你昨天已经问过这个问题,更新它然后得到答案stackoverflow.com/questions/31586794/…
  • 没有它不同的问题。在那里我问如何从 JSON 中获取值。在这里我问如何加载到tableview @developer
  • 有什么异常?似乎您正在创建 tableviewArray 作为循环内的局部变量。一旦循环结束,这将被释放。您需要将结果存储到属性或 iVar 中。您的循环也没有任何意义,因为只有最后一个数组将分配给 tableviewArray - 较早的数组引用将被覆盖
  • 有什么例外?
  • -[__NSCFString count]:无法识别的选择器发送到实例 0x7966c8b0 2015-07-24 10:39:49.477 Mvp_Discern[1125:19054] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: '-[__NSCFString count]: 无法识别的选择器发送到实例 0x7966c8b0'

标签: ios objective-c json uitableview


【解决方案1】:

我认为你得到了异常,因为当这个视图控制器被加载时你还没有初始化它。因为,它也会加载UITableView,并且您至少应该返回 0 以便在完成加载后让它显示一个空的表视图。对于您的情况,它甚至不返回 0,因为您的 tableviewArray 为零。所以,你应该在加载完成后调用 reload。

【讨论】:

    【解决方案2】:

    在 .h 文件中声明 colorArray 并在 viewDidLoad 方法中分配初始化它。 作为

    - (void)viewDidLoad {
    [super viewDidLoad];
    colorArray = [[NSMutableArray alloc]init];
    }
    

    添加颜色数组如下:-

      NSMutableArray 
     for (NSString * obj in key) {
    
     // Here below values I want to apply for tableview array
     NSLog(@"%@",jsonDictionary[@"response"][obj][@"color"]);
    
    [colorArray addObject:dict[@"response"][obj][@"Color"]]; // here I am applying array
    }
    

    那么你可以在tableview委托方法中访问colorArray。

    【讨论】:

    • 不好。 tableviewArray1 = [jsonDictionary[@"response"][obj][@"color"]]; // 不兼容的指针数组。制造不好的例外。 @pooja
    • 现在检查。我已经进行了更改。
    • [jsonDictionary[@"response"][obj][@"color"]]; // 预期的标识符错误。此外,在 tableviewArray =[[NSMutableArray alloc] initWithArray: tableviewArray1] 处检查 NSArray 和错误接收的所有右括号; @pooja
    • 没有。制造不好的例外。原因:'-[__NSCFNumber count]:无法识别的选择器发送到实例 0x78735380'@poojathorat
    • 可能是您在 tableview 的 cellforRowatIndexpath 委托方法中将 NSNumber 分配给 NSString
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多