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
  • Extravaganza 比萨 15 里拉
  • 意大利辣香肠披萨 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的datasource为新结构

#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 回答