2016-08-22 66 views
0

我從網絡服務獲得this json,我需要根據「PRICELISTCATEGORY」值對其進行分組。我嘗試了下面的代碼,但是我在表格中得到了重複的行和部分。我收集了self.arrayPriceList中的Web服務陣列。我究竟做錯了什麼?如何將UITableView與JSON值中提到的部分分組

從JSON Web服務收集陣列後,我打電話[self didReceiveResponseJson:self.arrayPriceList];

-(NSMutableDictionary *)priceListCategoryDitionaryAllReadyExist:(NSString *)price { 

    for(NSMutableDictionary *priceListDict in self.arrayPriceList){ 
     if([[[priceListDict objectForKey:@"PRICELISTCATEGORY"] objectForKey:@"text"] isEqualToString:price]) 
      //return the existing array refrence to add 
      return priceListDict; 
    } 
    // if we dont found then we will come here and return nil 
    return nil; 
} 

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

    for(NSDictionary *priceDict in jsonArray) { 

     NSMutableDictionary *existingPriceListDict=[self priceListCategoryDitionaryAllReadyExist:[[priceDict objectForKey:@"PRICELISTCATEGORY"] objectForKey:@"text"]]; 
     NSMutableArray *existingTempArray = [NSMutableArray array]; 
     if(existingPriceListDict != nil) { 
      //if name exist add in existing array.... 
      [existingTempArray addObject:priceDict]; 
     } 
     else { 
      // create new price list array 
      NSMutableArray *newPriceListArray=[[NSMutableArray alloc] init]; 
      // Add name dictionary in it 
      [newPriceListArray addObject:priceDict]; 

      // add this newly created pricelist array in globalNameArray 
      [self.arrayPriceList addObject:newPriceListArray]; 
     } 
    } 

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    TGAPriceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TGAPriceListCellId" forIndexPath:indexPath]; 
    NSDictionary *dict; 
    if (self.isFiltered) { 
     dict = [self.arrayFilteredPriceList objectAtIndex:indexPath.row]; 
    } else { 
     dict = [self.arrayPriceList objectAtIndex:indexPath.section]; 
    } 
    cell.lblAPNBarCode.text = [[dict objectForKey:@"APNBARCODE"] objectForKey:@"text"]; 
    cell.lblAvgCost.text = [[dict objectForKey:@"AVERAGECOST"] objectForKey:@"text"]; 

    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (self.isFiltered) { 
     return self.arrayFilteredPriceList.count; 
    } else { 
     NSArray *arrayPrice = [self.arrayPriceList objectAtIndex:section]; 
     return [arrayPrice count]; 
    } 

} 

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

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

    NSDictionary *arrayPrice = [self.arrayPriceList objectAtIndex:section]; 
    if([arrayPrice count]) { 
     return [[arrayPrice objectForKey:@"PRICELISTCATEGORY"] objectForKey:@"text"]; 
    } 
    else 
     return nil; 
} 
+0

從哪裏更改後你正在調用tableView.reload函數? – Sofeda

+0

在獲取json響應後的viewDidLoad中。 –

+0

@SMi請參閱我的編輯。 –

回答

0

調用didReceiveResponseJson在viewDidLoad中

self.arrayPriceList = [NSMutableArray array]; 
self.dictPriceList = [NSMutableDictionary dictionary]; 

我做出的tableview數據源的方法

-(void)didReceiveResponseJson:(NSMutableArray *)jsonArray { 
     for (NSDictionary *dict in jsonArray) { 
      NSString *strPriceListCategory = [[dict objectForKey:@"PRICELISTCATEGORY"] objectForKey:@"text"]; 
      if ([[self.dictPriceList allKeys] containsObject:strPriceListCategory]) { 
       NSMutableArray *arrayTemp = [self.dictPriceList objectForKey:strPriceListCategory]; 
       [arrayTemp addObject:dict]; 
       [self.dictPriceList setObject:arrayTemp forKey:strPriceListCategory]; 
      } else { 
       NSMutableArray *arrayTemp = [[NSMutableArray alloc] initWithObjects:dict, nil]; 
       [self.dictPriceList setObject:arrayTemp forKey:strPriceListCategory]; 
      } 
     } 
    } 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    TGAPriceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TGAPriceListCellId" forIndexPath:indexPath]; 
    NSDictionary *dict; 
    if (self.isFiltered) { 
     dict = [self.arrayFilteredPriceList objectAtIndex:indexPath.row]; 
    } else { 
     NSArray *arrayPriceListAllKeys = [self.dictPriceList allKeys]; 
     NSArray *arrayPrice = [self.dictPriceList objectForKey:[arrayPriceListAllKeys objectAtIndex:indexPath.section]]; 
     dict = [arrayPrice objectAtIndex:indexPath.row]; 
    } 
    cell.lblAPNBarCode.text = [[dict objectForKey:@"APNBARCODE"] objectForKey:@"text"]; 
    cell.lblAvgCost.text = [[dict objectForKey:@"AVERAGECOST"] objectForKey:@"text"]; 

    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (self.isFiltered) { 
     return self.arrayFilteredPriceList.count; 
    } else { 
     NSArray *arrayPriceListAllKeys = [self.dictPriceList allKeys]; 
     NSArray *arrayPrice = [self.dictPriceList objectForKey:[arrayPriceListAllKeys objectAtIndex:section]]; 
     return [arrayPrice count]; 
    } 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [[self.dictPriceList allKeys] count]; 
} 

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

    NSString *price = [[self.dictPriceList allKeys] objectAtIndex:section]; 
    return price; 
}