如何使用JSON值中提到的部分对UITableView进行分组

时间:2016-08-22 03:34:19

标签: ios uitableview

我从网络服务获得this json,我需要根据“PRICELISTCATEGORY”值对其进行分组。我尝试了以下代码,但是我在表中重复了一些行和部分。我在self.arrayPriceList中收集了Web服务数组。我做错了什么?

从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;
}

1 个答案:

答案 0 :(得分:0)

在viewDidLoad

中调用didReceiveResponseJson之后
    -(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;
}

我在tableview数据源方法中进行了更改

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.home1);  //icon appears in device notification bar and right hand corner of notification
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.home1));        
    builder.setContentIntent(pendingIntent);      
            builder.setContentTitle(Title);        
    builder.setContentText(message);        
    builder.setSubText(subtext);
    NotificationManager notificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);        
    notificationManager.notify(NOTIFICATION_ID, builder.build());