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' が原因でアプリを終了するなどのエラーが発生します。理由: '-[__NSCFString stringValue]: 認識されないセレクターがインスタンス 0x109314510 に送信されまし た'

4

1 に答える 1

0

タイプのキーとIDのキーを持つ辞書のNSArrayを取得する方が良いと思いますので、次のよう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 に答える