1

Webサービスを呼び出して、見返りに素敵なJSONを取得します。このJSONには、カテゴリ付きのレポートがいくつかリストされています。

大きな問題は、これを使用して、カテゴリ別にグループ化された見栄えの良いテーブルビューを作成する方法です。私はiOSを初めて使用しますが、この時点で本当に行き詰まっています。

jsonを次のような配列に保存します。

tableData = [NSJSONSerialization JSONObjectWithData:dataWebService options:kNilOptions error:&error];

そして、リストを並べ替えます。

NSArray *sortedArray;
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Category"  ascending:YES];
sortedArray = [tableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

私が得るjsonはこれです:

{
        Category = Faktura;
        about = "Fakturablankett med giro med utvalg p\U00e5 fra-til fakturanr";
        name = Faktura;
        reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
        reportid = 16;
    },
            {
        Category = Faktura;
        about = "Fakturablankett med giro med utvalg p\U00e5 fra-til fakturanr";
        name = "Faktura med sidenummer";
        reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
        reportid = 19;
    },
            {
        Category = Faktura;
        about = "Liste over fakturaer med status og mva-detaljer. Utvalg p\U00e5 fra-til fakturanr.";
        name = Fakturajournal;
        reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
        reportid = 15;
    },
            {
        Category = "Journaler og Kontoutskrifter";
        about = "";
        name = "Kontoutskrift hovedbok";
        reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
        reportid = 4;
    },
            {
        Category = "Journaler og Kontoutskrifter";
        about = "";
        name = "Kontoutskrift kunder";
        reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
        reportid = 5;
    }

これらの「名前」を「カテゴリ」ごとにグループ化してテーブルビューに一覧表示したいと思います。カテゴリを並べ替えて、これらのカテゴリに属する​​レポートを一覧表示する必要があります。

他にもたくさんのカテゴリがありますが、すべてを貼り付けることはしませんでした。

4

1 に答える 1

4

カテゴリ配列のリストを持つグローバル配列を1つ作成する必要があります。

category-arrays、used didReceiveResponseJson:nameDitionaryAllReadyExist:methodsと呼ばれる要素を持つグローバル配列を作成します。

UITableViewで上記のjsonデータを表示するには:

NSMutableArray* tableArray;//Declare this array globally and allocate memory in viewdidload

-(NSMutableArray *)nameDitionaryAllReadyExist:(NSString *)name {

    for(NSMutableArray *nameArray in tableArray){

        for(NSDictionary* nameDict in nameArray) {
            if([[nameDict objectForKey:@"Category"] isEqualToString:name])

                //return the existing array refrence to add
                return nameArray;
        }
    }

    // if we dont found then we will come here and return nil
    return nil;
}

-(void)didReceiveResponseJson:(NSArray *)jsonArray {

    for(NSDictionary* nameDict in jsonArray) {

        NSMutableArray* existingNameArray=[self nameDitionaryAllReadyExist:[nameDict objectForKey:@"Category"]];
        if(existingNameArray!=nil) {
            //if name exist add in existing array....
            [existingNameArray addObject:nameDict];
        }
        else {
            // create new name array
            NSMutableArray* newNameArray=[[[NSMutableArray alloc] init] autorelease];
            // Add name dictionary in it
            [newNameArray addObject:nameDict];

            // add this newly created nameArray in globalNameArray
            [tableArray addObject:newNameArray];
        }
    }

    //so at the end print global array you will get dynamic array with the there respetive dict.
    NSLog(@"Table array %@", tableArray);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    tableArray=[[NSMutableArray alloc] init];

    NSString* path=[[NSBundle mainBundle] pathForResource:@"JsonData" ofType:@"json"];

    NSDictionary* tableData=[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:kNilOptions error:nil];

    //NSLog(@"Table Array- %@",tableData);

    NSArray* dataArray = [tableData objectForKey:@"data"];

    [self didReceiveResponseJson:dataArray];
}

#pragma mark
#pragma mark UITableViewDataSource

//@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray* array=[tableArray objectAtIndex:section];
    return [array count];
}

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

        NSArray* array=[tableArray objectAtIndex:indexPath.section];

        NSDictionary* item=[array objectAtIndex:indexPath.row];
        NSString* name=[item valueForKey:@"name"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil) {
        // No cell to reuse => create a new one
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];        
        // Initialize cell
    }

    // Customize cell
    cell.textLabel.text = name;

    return cell;
}

//@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [tableArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

        NSArray* array=[tableArray objectAtIndex:section];
    if([array count]) {
        NSDictionary* item=[array objectAtIndex:0];
    return [item valueForKey:@"Category"];
    }
    else
        return nil;
}
于 2012-11-22T20:20:28.363 に答える