繁体   English   中英

如何获取json数据并将Array Integer字典值打印到我的tableview单元格中

[英]how to get json data and print Dictionary of Array Integer value into my tableview cell

我有json数据,例如

{
NetworkMembers =         (
                {
        id = 1;
        memberId = 1;
        networkId = 1;
        position = 0;
        type = H;
    },

最后,我将数组放入这样:

for (int i=0; i<[array count]; i++) { 
    NSDictionary *dictionary = [array objectAtIndex:i]; 
    NSArray *array2=[dictionary valueForKey:@"NetworkMembers"]; 
    for (int j=0; j<[array2 count]; j++) { 
        NSDictionary *dictionary2 = [array2 objectAtIndex:j]; 
        NSLog(@"J value %@",[dictionary2 valueForKey:@"type"]); 
        [self.tablArray addObject:[dictionary2valueForKey:@"type"]]; 
        [self.tablArray addObject:[dictionary2 valueForKey:@"id"]]; 
    } 
} 

最后我得到像tableArray(H,1,H,2,H,3,H,4,H,6然后在单元格中为索引路径的行我写了像

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testing"];


if(!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"testing"];

}
cell.textLabel.text = [tablArray objectAtIndex:indexPath.row];
if([tablArray count]>0)
{
           cell.detailTextLabel.text=[[tablArray objectAtIndex:indexPath.row]stringValue];
}

return cell;

像终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因,但打印的详细文本拉布勒IAM越来越错误:“ - [__ NSCFString stringValue的]:无法识别的选择发送到实例0x109314510” *第一掷调用堆栈:预先感谢任何人可以帮助我

我认为最好使用字典的NSArray,其中包含用于类型的键和用于ID的键,因此tableArray将为:

// Create a property of tableArray to retrieve data later
@property (nonatomic, strong) NSMutableArray *tableArray

// Add data inside your tableArray your viewDidLoad
NSDictionary *dicAllNetworks = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; // Retrieve data from your JSON file inside a NSDictionary

_tableArray = [NSMutableArray array];
for (NSDictionary *dic in [dicAllNetworks objectForKey:@"NetworkMembers"])
{
    [tableArray addObject:@{@"id" : [dic objectForKey:@"id"], @"type": [dic objectForKey:@"type"]}];
}
NSLog(@"%@", tableArray);

然后您将得到如下数组:

tableArray (
        {
        id = 1;
        type = H;
    },
        {
        id = 2;
        type = H;
    }
)

然后像这样修改您的代码:

cell.textLabel.text = [[tablArray objectAtIndex:indexPath.row] objectForKey:@"type"];
if([tablArray count]>0)
{
           cell.detailTextLabel.text=[[tablArray objectAtIndex:indexPath.row] objectForKey:@"id"];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM