1

JSON データを使用した WCF サービスを使用するアプリの開発を開始しました。WCF サービスからデータを取得しましたが、思いどおりに使用できませんでした。

JSONデータは次のとおりです。

{"MenuDoldurAndroidResult":[
{"menu_description":"Turkish Pizza","menu_title":"L Pizza","menu_price":"26 TL"},{"menu_description":"Italiano Pizza","menu_title":"L Pizza","menu_price":"27 TL"},{"menu_description":"Extravaganza","menu_title":"L Pizza","menu_price":"29 TL"},{"menu_description":"Pepporoni Pizza","menu_title":"L Pizza","menu_price":"28 TL"},{"menu_description":"Turkish Pizza","menu_title":"S Pizza","menu_price":"12 TL"},{"menu_description":"Italiano Pizza","menu_title":"S Pizza","menu_price":"13 TL"},{"menu_description":"Extravaganza","menu_title":"S Pizza","menu_price":"15 TL"},{"menu_description":"Pepporoni Pizza","menu_title":"S Pizza","menu_price":"14 TL"}
]}

私が欲しいもの:

ここに 2 つのタイトルがある場合、テーブル ビューには 2 つのセクションが必要です。すべての項目がそれぞれのセクションにある必要があります。

このような:

-L ピザ

  • トルコ風ピザ 26 TL
  • イタリアーノ ピザ 27 TL
  • エクストラバガンザ ピザ 29 TL
  • ペパロニ ピザ 28 TL

-S ピザ

  • トルコ風ピザ 12 TL
  • イタリアーノ ピザ 13 TL
  • エクストラバガンザ ピザ 15 TL
  • ペパロニ ピザ 14 TL

このアイテムにアクセスして、このように表示するにはどうすればよいですか?

- (void)viewDidLoad
{
    [super viewDidLoad];

//I posted request to service here. I didn't write these parts of code.

NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:NSJSONReadingMutableContainers
                              error:&error];

        NSMutableArray *array= [json objectForKey:@"MenuDoldurAndroidResult"];

        menu = [[NSMutableArray alloc] initWithCapacity:3];

        NSString *descriptionTemp;
        NSString *titleTemp;
        NSString *priceTemp;
        for(int i=0; i< array.count; i++)
        {
            NSDictionary *menuList= [array objectAtIndex:i];
            titleTemp = [menuList objectForKey:@"menu_title"];
            descriptionTemp = [menuList objectForKey:@"menu_description"];
            priceTemp = [menuList objectForKey:@"menu_price"];
            [menu addObject:[NSArray arrayWithObjects:titleTemp,descriptionTemp,priceTemp,nil]];

        }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

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

    if (section==0) {
        return @"L Pizzas";
    }
    else{
        return @"S Pizzas";
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.textLabel.text = [menu objectAtIndex: indexPath.row];
    return cell;
}
4

2 に答える 2

4

コンテンツが静的な場合は、Sunnyの回答を使用してみてください。ただし、が動的である場合は、データを別の方法で保存することをお勧めします。明らかに、LピザとSピザはカテゴリのようで、残りはカテゴリアイテムのようです。

カテゴリのコレクションを作成する必要があります。デモプロジェクトのソースコード

- (void)viewDidLoad
{
    [super viewDidLoad];

//I posted request to service here. I didn't write these parts of code.

NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:NSJSONReadingMutableContainers
                              error:&error];

NSMutableArray *allPizzas = [json[@"MenuDoldurAndroidResult"] mutableCopy];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"menu_price"
                                                                 ascending:YES
                                                                  selector:@selector(compare:)];
[allPizzas sortUsingDescriptors:@[sortDescriptor]];

NSMutableArray *pizzaCategories = [@[]mutableCopy];
//Find unique categories in all the pizzas
NSSet* categories = [NSSet setWithArray: [allPizzas valueForKey:@"menu_title"]];

//Enumerate to form a new reformatted category array
for (NSString *categoryTitle in categories)
{
    //Predicate is used to find the items that come under current category
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"menu_title == %@",categoryTitle];
    NSArray *categoryItems = [allPizzas filteredArrayUsingPredicate:predicate];
    //New dictionary with name of category and category items are formed 
    NSDictionary *categoryDict = @{@"menu_title":categoryTitle,@"pizzas":categoryItems};
    [pizzaCategories addObject:categoryDict];

}

//Assign the new formatted category array to the instance variable for holding categories.
self.categories = pizzaCategories;
}

新しい構造のtableViewのデータソースを変更します

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   //The count of categories will give number of sections
    NSUInteger sections = [self.categories count];
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //The number of items in a category is calculated
    NSDictionary *category = self.categories[section];
    return [category[@"pizzas"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //Title for the category
    NSDictionary *category = self.categories[section];
    return category[@"menu_title"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSDictionary *category      = self.categories[indexPath.section];
    NSArray *categoryItems      = category[@"pizzas"];
    NSDictionary *categoryItem  = categoryItems[indexPath.row];

    cell.textLabel.text = categoryItem[@"menu_description"];
    cell.detailTextLabel.text = categoryItem[@"menu_price"];

    return cell;
}
于 2013-03-22T15:06:50.523 に答える
0

また、無料の Sensible TableView フレームワークを使用して Web サービスからデータを取得し、テーブル ビューに自動的に表示することもできます。

于 2013-03-23T22:10:53.973 に答える