通过UITableView部分重置值

问题描述 投票:0回答:1

我正在构建一个个人预算应用程序来跟踪我的购买。我的NSDictionaryNSArray,如下所示:

{
    46 =    (
                {
            DATE = "11/14/2019";
            ID = 21;
            PLACE = Kroger;
            PRICE = "23.32";
        },
                {
            DATE = "11/14/2019";
            ID = 22;
            PLACE = Walmart;
            PRICE = "12.54";
        }
    );
    47 =     (
                {
            DATE = "11/18/2019";
            ID = 23;
            PLACE = Cinco;
            PRICE = "65.32";
        }
    );
}

NSDictionary中的键由strftime使用%W确定。基本上,我有一个每周可以使用的上限(即190),并且我希望每个titleForHeaderInSection:的总值由每个键中每个字段的PRICE确定。我目前在应用程序上具有以下内容:App

以下将计算上面列出的NSDictionary以及总值:注意:arrFoodInfo是just项的NSDictionary,没有星期数

for (NSDictionary *dict in self.arrFoodInfo) {
        NSString *date = [dict objectForKey:@"DATE"];
        [totalPrices addObject:[dict objectForKey:@"PRICE"]];

        for (NSString *price in totalPrices) {
            if ([totalPrices containsObject:price]) {
                _sumPrices += [price floatValue]; // Calculates total value
                [totalPrices removeObject:price];
            }
        }

        NSDateFormatter *SDF = [[NSDateFormatter alloc]
                                 init];
        [SDF setDateStyle:NSDateFormatterMediumStyle];
        [SDF setDateFormat:@"MM/dd/yyyy"];
        NSDate *convertedDate = [SDF dateFromString:date];

        NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:convertedDate];

        NSInteger week = dateComponents.weekOfYear;
        NSInteger index = week;
        NSNumber *key = @(index);

        NSMutableArray *weekArray = weeksDictionary[key];
        if (!weekArray) {
            weekArray = [NSMutableArray array];
            weeksDictionary[key] = weekArray;
        }
        [weekArray addObject:dict];
    }

目前,我得到每个PRICE的总和,但每个部分的总价值应该有所不同。确定此的最佳选择是什么?

ios objective-c uitableview nsarray nsdictionary
1个回答
-1
投票

请在标头部分中显示您的代码以计算总价。

如果您对数据没有特殊要求,我建议您更改为here之类的结构。如果遵循此结构,则可以轻松处理每个节以及每个节的行的数据。

© www.soinside.com 2019 - 2024. All rights reserved.