2

次の JSON があるとします。

[
    {"x":"01", "ID":"1"},
    {"x":"02", "ID":"2"},
    {"x":"02", "ID":"3"},
    {"x":"03", "ID":"4"},
    {"x":"03", "ID":"5"},
    {"x":"03", "ID":"6"},
    {"x":"03", "ID":"7"}
]

そして、次のような UITableView を作成したい:

------------
Section 01
------------
ID: 1
------------
Section 02
------------
ID: 2
ID: 3
------------
Section 03
------------
ID: 4
ID: 5
ID: 6
ID: 7

必要なセクションの数と、各セクションで正しいデータを出力するにはどうすればよいですか?

4

3 に答える 3

1

最初にすべきことは、JSON を Objective-C のデータ構造に変換することです。「X」が「ID」値の各配列のインデックスである配列の配列をお勧めします。

何かのようなもの:

NSMutableArray *tableSections;
NSMutableArray *sectionData;

CustomDataObject *yourCustomDataObject;

int sectionIndex;

//Pseudo-code to create data structure
for(data in json) {

    sectionIndex = data.X;

    yourCustomDataObject = [[CustomDataObject alloc] initWithId:data.ID];

    //Do index check first to insure no out of bounds
    if(sectionIndex != OutOfBounds)
        sectionData = [tableSections objectAtIndex:sectionIndex];

    //Create the new section array if there isn't one for the current section
    if(!sectionData) {
        sectionData = [NSMutableArray new];
        [tableSections insertObject: sectionData atIndex: sectionIndex];
        [sectionData release];
    }

    [sectionData addObject: yourCustomDataObject];
    [yourCustomDataObject release];
}

上記のコードは、作業を開始するのに役立つ疑似コードです。この配列の配列の作成は、おそらく以前に行ったことがあるでしょう。

重要な部分は、UITableViewDataSource プロトコルを実装してこのデータにアクセスすることです。UITableView をサブクラス化し、カスタム サブクラスに UITableViewDataSource プロトコルを実装することをお勧めします。

次のメソッドが必要になります。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [tableSections count];
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[tableSections objectAtIndex: section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //Implement as you normally would using the data structure you created

    NSMutableArray *sectionData = [tableSections objectAtIndex: indexPath.section];
    CustomDataObject *dataObject = [sectionData objectAtIndex: indexPath.row];

    NSLog(@"\n**** Debug Me *****indexPath: section: %i row: %i \ndataObject%@", indexPath.section, indexPath.row, dataObject);
}

始めるにはこれで十分です。残りの詳細を把握することは、良い習慣になるはずです。

于 2012-12-19T00:12:10.590 に答える
0

この解決策で終わった:

    NSMutableDictionary *result = [NSMutableDictionary dictionary];
    for(NSDictionary* dict in data) {
        NSNumber *x = [dict objectForKey:@"x"];
        NSMutableArray *resultsForX = [result objectForKey:x];

        if(!resultsForX) {
            resultsForX = [NSMutableArray array];
            [result setObject:resultsForX forKey:x];
        }

        [resultsForX addObject:dict];
    }

    NSMutableArray *newArr = [result allValues];
于 2012-12-20T19:53:54.370 に答える
0

「x」の値が 1 から値をスキップせずに 1 まで増加すると仮定すると、n必要なセクションの数は単純に「x」の最後の値 (つまりn) になります。

そうでない場合は、「x」の値を繰り返し処理して、一意の値がいくつあるかを確認する必要があります。

于 2012-12-18T23:40:35.253 に答える