0

haiii 我有 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 出现类似 Terminating app 的错误,原因:“-[__NSCFString stringValue]: unrecognized selector sent to instance 0x109314510

4

1 回答 1

0

我认为最好获得一个 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"];
}
于 2013-10-14T13:26:30.247 回答