如何在每个uicollectionviewcell中分析不同的字典数组。

我有一个API请先看看这个。

{"result":"Successful","data":[{"price":"100.00","packs":"Garbage Bags","pack_id":"1","products":[{"quantity":"15","image":"1454651885ECOWGBS_03.jpg"},{"quantity":"15","image":"1454652017ECOWGBM_03.jpg"},{"quantity":"15","image":"1454652132ECOWGBL_02.jpg"}]},{"price":"200.00","packs":"Party","pack_id":"2","products":[{"quantity":"50","image":"1454589144USI_6819.jpg"},{"quantity":"50","image":"1454587252ecow240b_01.jpg"},{"quantity":"50","image":"1454499020ecow10rp_01.jpg"}]},{"price":"300.00","packs":"Travel","pack_id":"3","products":[{"quantity":"25","image":"1454589144USI_6819.jpg"},{"quantity":"25","image":"1454588615ecow103crp_01.jpg"},{"quantity":"25","image":"1455532004ECOWCLAM1_03.jpg"}]},{"price":"400.00","packs":"Kids","pack_id":"4","products":[{"quantity":"25","image":"1454499251ecow6rp_01.jpg"},{"quantity":"25","image":"1454588417ecow5ct_01.jpg"},{"quantity":"25","image":"1454587802ecow340b_01.jpg"}]}]} 

生成的单元格数量是根据api中的包数量生成的。 例如,在这个API中有四个包在那里,所以我parsing包的总数,并将其添加到数组,并添加像这样在uicollectionview数组。

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return [packdetail count]; 

}

直到现在所有的东西都正常工作,但是当我试图传递uilabel上的产品数据的数量时,它显示在每个单元格上,并且只显示第一个产品数组。 但我需要通过第一个单元格上的第一个产品数组和第二个单元格上的第二个产品数组,等等。 这是我的代码,请看这个。

  -(void)connectionDidFinishLoading:(NSURLConnection *)connection { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; NSArray *dataarray = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"]; [packdetail removeAllObjects]; for (NSDictionary *tmp in dataarray) { NSMutableDictionary *temp = [NSMutableDictionary new]; [temp setObject:[tmp objectForKey:@"packs"] forKey:@"packs"]; [temp setObject:[tmp objectForKey:@"pack_id"] forKey:@"pack_id"]; [packdetail addObject:temp]; NSLog(@"packdetail %@", packdetail); productarray = [tmp objectForKey:@"products"]; NSLog(@"products %@", productarray); for (NSDictionary *product in productarray) { NSMutableDictionary *temp2 =[NSMutableDictionary new]; [temp2 setObject:[product objectForKey:@"quantity"]forKey:@"quantity"]; [quantity addObject:temp2]; } } if (packdetail) { [_subscriptioncollectionview reloadData]; } 

}

  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return [packdetail count]; } -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ SubscriptionCell *cell = (SubscriptionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.packname.text = [[packdetail objectAtIndex:indexPath.row] objectForKey:@"packs"]; cell.productname.text = [[quantity objectAtIndex:indexPath.row] objectForKey:@"quantity"]; return cell; } 

你的答案是

 for (NSDictionary *tmp in dataarray) { NSMutableDictionary *temp = [NSMutableDictionary new]; [temp setObject:[tmp objectForKey:@"packs"] forKey:@"packs"]; [temp setObject:[tmp objectForKey:@"pack_id"] forKey:@"pack_id"]; [packdetail addObject:temp]; NSLog(@"packdetail %@", packdetail); productarray = [tmp objectForKey:@"products"]; NSLog(@"products %@", productarray); for (NSDictionary *product in productarray) { NSMutableDictionary *temp2 =[NSMutableDictionary new]; [temp2 setObject:[product objectForKey:@"quantity"]forKey:@"quantity"]; [quantity addObject:temp2]; } } 

改成

 for (NSDictionary *tmp in dataarray) { NSMutableDictionary *temp = [NSMutableDictionary new]; [temp setObject:[tmp objectForKey:@"packs"] forKey:@"packs"]; [temp setObject:[tmp objectForKey:@"pack_id"] forKey:@"pack_id"]; NSLog(@"packdetail %@", packdetail); productarray = [tmp objectForKey:@"products"]; NSLog(@"products %@", productarray); for (NSDictionary *product in productarray) { [temp setObject:[product objectForKey:@"quantity"]forKey:@"quantity"]; } [packdetail addObject:temp]; } 

在单元格中显示

 cell.label.text = [[packdetail objectAtIndex:indexPath.row] objectForKey:@"quantity"];; 
 NSArray *array = [JsonArray]; 

在CellForRowAtIndexPath中:

 cell.label.text = [[[array objectAtIndex:indexPath.section] objectForKey:@"products"][indexpath.row]valueForkey @"quantity"];