繁体   English   中英

在表视图控制器中显示之前对JSON响应进行排序-目标C

[英]Sorting JSON Response before show in table view controller - Objective C

Rewards =     (
                {
            "Restaurant_ID" = 34;
            "Rewarded_Points" = 40;
            "Rewarded_Time" = "2015-11-23 09:16:00";
        },

           {
            "Restaurant_ID" = 40;
            "Rewarded_Points" = 60;
            "Rewarded_Time" = "2015-11-24 12:22:56";
          },
            {
            "Restaurant_ID" = 34;
            "Rewarded_Points" = 40;
            "Rewarded_Time" = "2015-11-24 12:22:56";
        }
);

我检索上面对变量的响应,并使用for循环显示在TableViewController中

for(int i=0;i<rewards.count;i++){
        NSDictionary * rewardsObj = [rewards objectAtIndex:i];
        restId = [rewardsObj objectForKey:@"Restaurant_ID"];
        rewardedPoints = [rewardsObj objectForKey:@"Rewarded_Points"];
}
[self.tableView reloadData];

当前输出一次又一次显示相同的餐厅ID。 我想如果有匹配的ID,则这些点需要计算总和。

34 - Total Points : 40

40 - Total Points : 60

34 - Total Points : 40 //

但是我想像下面这样排序

34 - Total Points : 80 // 40 + 40

40 - Total Points : 60

使用NSSortDescriptor作为您的概念,

第1步

首先根据sort选择键,对于我们取的Rewarded_Pointsascending排序。

NSSortDescriptor * Rewarded_Points =
[[NSSortDescriptor alloc] initWithKey:@"Rewarded_Points"
                           ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:Rewarded_Points, nil];

 NSArray *sortedArrayOfDictionaries = [Rewards sortedArrayUsingDescriptors:descriptors]; // Rewards -> array of dictionary name

NSLog(@"final Value : %@", sortedArrayOfDictionaries);

有关其他教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM