2012-01-12 6 views
0

tableView numberofRowsinSectionを実装する方法を理解しようと夢中になり、ヒントや提案を得ることができたらと思っていました。私は全体を複雑すぎるように感じる。今すぐNSDictionaryに変換されたxmlデータを投稿してみましょう。_NSCFString allKeys、セレクタが認識されません。私はそれがNSDictionaryだと思った?

{ 
    "@body" = ""; 
    "@class_id" = ""; 
    "@title" = Discussions; 
    "@type" = "Discussion Block"; 
}, 
{ 
    "@body" = ""; 
    "@class_id" = ""; 
    "@col" = 1; 
    "@title" = YouTube; 
    "@type" = "Youtube Block"; 
    item =  { 
     "@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e"; 
     "@title" = "Gavin DeGraw - Not Over You"; 
     "@type" = link; 
    }; 
}, 
{ 
    "@body" = ""; 
    "@class_id" = ""; 
    "@title" = Calendar; 
    "@type" = "Calendar Block"; 
}, 
{ 
    "@body" = "<p> homework test</p> "; 
    "@class_id" = ""; 
    "@title" = Homework; 
    "@type" = "File Block"; 
    item =  (
       { 
      "@body" = "https://MYDOMAIN.com/securelink; 
      "@title" = "crashreport.rtf"; 
      "@type" = file; 
     }, 
       { 
      "@body" = "mydomain.com/securelink"; 
      "@title" = "Chem notes sep 26.docx"; 
      "@type" = file; 
    } 
); 
}, 
{ 
    "@body" = "<p> Link testing</p> "; 
    "@class_id" = ""; 
    "@title" = Links; 
    "@type" = "Link Block"; 
    item =  (
       { 
      "@body" = "http://google.com"; 
      "@title" = Link1; 
      "@type" = link; 
     }, 
       { 
      "@body" = "http://yahoo.com"; 
      "@title" = link2; 
      "@type" = link; 
     }, 
       { 
      "@body" = "http://reddit.com"; 
      "@title" = link3; 
      "@type" = link; 
     } 
    ); 
} 
) 

上記のデータはレスポンスです。これの目標は、最初のタイトルキーをテーブルヘッダーにし、2番目の "タイトル"キー(内部 "アイテム")をそれぞれのセクションの行にすることです。 tableView TitleforHeaderinSection方法では、私は単に物事はトリッキー取得する場所numberofRowsinSection方法がある

for (NSDictionary *roster in response) { 
    [blockName addObject:[roster [email protected]"@title"]]; 
} 
    return [blockName objectAtIndex:section]; 

このコードを使用します。これは私が試したものです

for (NSDictionary *myDict in [[response objectAtIndex:section] valueForKeyPath:@"item"]) { 
    NSArray *keys = [myDict allKeys]; 
    if ([keys containsObject:@"@title"]) { 
     array3 = [[NSMutableArray alloc] init]; 
     [array3 addObject:[myDict objectForKey:@"@title"]]; 
    } 
} 
return array3; 

私が得ているエラーは、myDictがNSStringであり、allKeysを使用できないということです。私はobjectAtIndex:1に変更してallKeysメソッドを動作させるので、なぜこれが起こっているのか分かりません。私はまた、これが各セクションに必要な行の数を見つける最良の方法ではないことを確かめているので、もっと良い方法があれば分かち合いましょう。ただ、明確にする

が、これは私が上記のデータを解析した後、私のtableViewで探しているものです:あなたの助けのための

Header (Discussions) 
    -No rows under this header 
Header (YouTube) 
    -Row (Gavin DeGraw - Not Over You) 
Header (Calendar) 
    -No rows 
Header (Homework) 
    -Row (crashreport) 
    -Row (chem notes) 
Header (links) 
    -Row (link1) 
    -Row (link2) 
    -Row (link3) 

感謝。私はNSDictionaryとNSArrayメソッドについて多くの研究を行ってきましたが、この問題を解決する方法やこれ以外の方法でコードする方法がわかりません。

答えて

0

問題はここにある。ため

(中NSDictionaryの* myDict [[応答objectAtIndex:セクション] valueForKeyPath: "項目" @]){

これは辞書のために問題ありこれは、NSDictionaryオブジェクトのNSArrayをループするためです。したがって、[myDict allKeys]が呼び出されると、実際にはNSDictionaryで呼び出されます。

{ 
    "@body" = "<p> homework test</p> "; 
    "@class_id" = ""; 
    "@title" = Homework; 
    "@type" = "File Block"; 
    item =  (
       { 
      "@body" = "https://MYDOMAIN.com/securelink; 
      "@title" = "crashreport.rtf"; 
      "@type" = file; 
     }, 
       { 
      "@body" = "mydomain.com/securelink"; 
      "@title" = "Chem notes sep 26.docx"; 
      "@type" = file; 
    } 
); 

しかし、あなたは、配列の代わりに辞書をループしているので、それは次の辞書の罰金ではないので、このfor..inループは、辞書のキーをループしています。次に、NSString(辞書のキーとも呼ばれます)でallKeysを呼び出しています。

{ 
    "@body" = ""; 
    "@class_id" = ""; 
    "@col" = 1; 
    "@title" = YouTube; 
    "@type" = "Youtube Block"; 
    item =  { 
     "@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e"; 
     "@title" = "Gavin DeGraw - Not Over You"; 
     "@type" = link; 
    }; 
}, 

あなたはこの代わりのような何かができる:

id item = [[response objectAtIndex:section] valueForKeyPath:@"item"]; 
if(item) { 
    if([item isKindOfClass:[NSDictionary class]]) { 
     NSArray *keys = [(NSDictionary *)item allKeys]; 
    } else if([item isKindOfClass:[NSArray class]]) { 
     for (NSDictionary *myDict in (NSArray *)item) { 
      NSArray *keys = [myDict allKeys]; 
     } 
    } 
} 
else { 
    return 0; 
} 
+0

[OK]を、理にかなって、ありがとう。だから私が使用しているメソッドは、ヘッダーごとに2つ以上の項目がある場合にのみ動作するだろうか?このテーブルを設定する別の方法がありますか? – davis

+0

私の応答を編集してください –

+0

すごく、ありがとう。 else {return 0}を追加して、valueforkeypathという項目がない行を表示しないようにしました。 – davis

0

はのは、「アイテム」キーが存在する場合、対応するオブジェクトはアイテムのNSArrayのであると仮定しよう。

試してみてください。

NSArray *items = [[response objectAtIndex:section] valueForKey:@"item"]; 

if (items!=nil) { 
    return [items count]; 
} else { 
    return 0; 
} 
関連する問題