如何在每年的基础上制作UITableView部分,并在每个部分的行中按月填充数据

时间:2015-06-04 21:59:22

标签: ios objective-c uitableview

我从SQLite3数据库获取以下数据数组。

array (
    {
    amount = "$100";
    balance = "$1505";
    date = "06/22/2015";
    id = 16;
    note = Pay;
    type = Pay;
},
    {
    amount = "$1000";
    balance = "$1405";
    date = "06/22/2015";
    id = 15;
    note = Pay;
    type = Pay;
},
    {
    amount = "$200";
    balance = "$405";
    date = "06/22/2015";
    id = 14;
    note = Pay;
    type = Pay;
},
    {
    amount = "$100";
    balance = "$205";
    date = "06/22/2015";
    id = 13;
    note = Pay;
    type = Pay;
},
    {
    amount = "$100";
    balance = "$105";
    date = "06/22/2015";
    id = 12;
    note = Pay;
    type = Pay;
},
    {
    amount = "$50";
    balance = "$5,320.00";
    date = "06/16/2015";
    id = 11;
    note = Pay;
    type = Pay;
},
    {
    amount = "$50";
    balance = "$5,270.00";
    date = "06/09/2015";
    id = 10;
    note = Pay;
    type = Pay;
},
    {
    amount = "$50";
    balance = "$5220";
    date = "06/02/2015";
    id = 9;
    note = Pay;
    type = Pay;
},
    {
    amount = "$100";
    balance = "$5170";
    date = "06/03/2015";
    id = 8;
    note = Pay;
    type = Pay;
},
    {
    amount = "$100";
    balance = "$5070";
    date = "06/02/2015";
    id = 7;
    note = Pay;
    type = Pay;
},
    {
    amount = "$20";
    balance = "$4970";
    date = "05/29/2015";
    id = 6;
    note = water;
    type = Deposit;
},
    {
    amount = "$100";
    balance = "$4950";
    date = "05/29/2015";
    id = 5;
    note = water;
    type = Expense;
},
    {
    amount = "$50";
    balance = "$5050";
    date = "05/29/2015";
    id = 4;
    note = Pay;
    type = Pay;
}

我想在每年的基础上制作一个包含部分的表格视图,并且在执行收入和费用计算后,每个部分数据将按月显示。有关更多信息,我附上了一个屏幕截图。 enter image description here

我试图使用以下方法解决问题。

-(NSMutableArray*)arrangeSection:(NSMutableArray *)source
{
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setLocale:[NSLocale currentLocale]];
    [_formatter setDateFormat:@"YYYY"];
    NSMutableArray *arrayMain=[NSMutableArray array];
    for (int i=0; i<source.count; i++){
    NSDictionary *dict=source[i];
    NSDateFormatter *_formatterLocal=[[NSDateFormatter alloc]init];
    [_formatterLocal setLocale:[NSLocale currentLocale]];
    [_formatterLocal setDateFormat:@"MM/dd/yyyy"];
    NSDate * date = [_formatterLocal dateFromString:[dict objectForKey:TABLE_DATE]];
    NSString *yy=[_formatter stringFromDate:date];
    NSMutableDictionary *secDict=[NSMutableDictionary dictionary];
    NSMutableArray *secArray=[NSMutableArray array];

    if (i==0){
        [secDict setObject:yy forKey:@"Year"];
        [secArray addObject:dict];
        [secDict setObject:secArray forKey:@"Data"];
        [arrayMain addObject:secDict];
    }
    else{
        BOOL flg=NO;
        for (NSDictionary *dict2  in arrayMain){
            if([[dict2 objectForKey:@"Year"]isEqualToString:yy]){
                flg=YES;
                [[dict2 objectForKey:@"Data"]addObject:dict];
                break;
            }
        }

        if (!flg){
            [secDict setObject:yy forKey:@"Year"];
            [secArray addObject:dict];
            [secDict setObject:secArray forKey:@"Data"];
            [arrayMain addObject:secDict];

        }
    }
}
return arrayMain;

}

2 个答案:

答案 0 :(得分:0)

如果我理解您的要求是正确的,那么您每年需要一个部分,每个部分有12行,每月一个。

这不应该太难实现:)。

我建议您制作JSON对象的模型:

{
    amount = "$20";
    balance = "$4970";
    date = "05/29/2015";
    id = 6;
    note = water;
    type = Deposit;
}

让我们将这个类称为&#34; MonthModel&#34;,然后创建一个&#34; YearModel&#34;持有12(或更少,如果没有可用于所述年份的数据)&#34; MonthModels&#34;。我在下面的未经测试的代码中发布了对模型结构的建议。

像:

YearModel

@interface YearModel()
@property (strong, nonatomic) NSArray *monthModels;
@property (strong, assign) NSInteger year;
@end

@implementation YearModel
- (instancetype)initModelForYear:(NSInteger)year withMonthModels:(NSArray*)monthModels {
    self = [super init];

    if(self) {
        self.year = year;
        self.monthModels = monthModels;
    }
    return self;
}
@end

您的MonthModel

@interface MonthModel()
@property (strong, assign) NSFloat balance;
@property (strong, assign) NSFloat amount;
@end

@implementation MonthModel
- (instancetype)initWithBalance:(NSFloat)balance andAmount:(NSFloat)amount {
    self = [super init];

    if(self) {
        self.balance = balance;
        self.amount = amount;
    }
    return self;
}
@end

如果您的UIViewController是您的UITableViewDataSource,那么它可以使用YearModels数组

@interface MyViewController() <UITableViewDataSource, UITableViewDelegate>
    @property (strong, nonatomic) IBOutlet UITableView *tableView;
    @property (strong, nonatomic) NSArray* yearModels;
    @end

    @implementation MyViewController

        - (MonthModel*)monthModelForIndexPath:(NSIndexPath*)indexPath {
            YearModel* yearModel = [yearModels objectAtIndex:indexPath.section];
            MonthModel* monthModel = [yearModel.monthModels objectAtIndex:indexPath.row];
            return monthModel;
        }

        #pragma mark - UITableViewDataSource
        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
            return [yearModels count];
        }

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            YearModel* yearModel = [yearModels objectAtIndex:section];
            return [yearModel.monthModels count];
        }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            static NSString *cellIdentifier = @"MonthCell";
            MonthCell *cell = (MonthCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            MonthModel* monthModel = [self monthModelForIndexPath:indexPath];
            [cell updateWithModel:monthModel];
            return cell;
        }

@end

希望您在代码中的某处创建一个从SQLite读取数据并创建上面建议的模型的方法并不是那么棘手。然后设置&#34; yearModels&#34; UIViewController中的属性。

但是,如果您需要一些SQLite的灵感,请告诉我 - &gt;模型转换。

祝你好运!

答案 1 :(得分:0)

我通过以下排序方法解决了我的问题。

-(NSMutableArray*)arrangeSection:(NSMutableArray *)source
{
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setLocale:[NSLocale currentLocale]];
    [_formatter setDateFormat:@"YYYY"];
    NSString *year = @"";
    NSString *month = @"";
    NSMutableArray *arrayMain=[[NSMutableArray alloc] init];
    NSMutableDictionary *lastDict = [[NSMutableDictionary alloc] init];
    NSMutableArray *lastArray = [[NSMutableArray alloc] init];
    for (int i=0; i<source.count; i++){
        NSDictionary *dict=source[i];  // Year data dictionary

        NSDateFormatter *_formatterLocal=[[NSDateFormatter alloc]init];
        [_formatterLocal setLocale:[NSLocale currentLocale]];
        [_formatterLocal setDateFormat:@"MM/dd/yyyy"];
        NSDate * date = [_formatterLocal dateFromString:[dict objectForKey:TABLE_DATE]];
        [_formatter setDateFormat:@"YYYY"];
        NSString *currentYear=[_formatter stringFromDate:date];
        if (![year isEqualToString:currentYear]) {
        if (i!=0) {
            [lastDict setObject:year forKey:@"Year"];
            [lastDict setObject:arrayMain forKey:@"Data"];
            [lastArray addObject:lastDict];
            lastDict = [[NSMutableDictionary alloc] init];
            arrayMain=[[NSMutableArray alloc] init];
        }
    }

    [_formatter setDateFormat:@"MMMM"];
    NSString *currentMonth = [_formatter stringFromDate:date];

    if (![month isEqualToString:currentMonth]) {
        NSMutableDictionary *secDict=[[NSMutableDictionary alloc] init];
        [secDict setObject:currentMonth forKey:TABLE_DATE];
        if ([[dict valueForKey:TABLE_TYPE] isEqualToString:@"Expense"]) {
           [secDict setObject:[dict valueForKey:TABLE_AMOUNT] forKey:TABLE_AMOUNT];
        }
        else
        {
            [secDict setObject:@"0" forKey:TABLE_AMOUNT];
        }
        if ([[dict valueForKey:TABLE_TYPE] isEqualToString:@"Deposit"])
        {
            [secDict setObject:[dict valueForKey:TABLE_AMOUNT] forKey:TABLE_NOTE];
        }
        else
        {
            [secDict setObject:@"0" forKey:TABLE_NOTE];
        }

        [secDict setObject:[dict valueForKey:TABLE_BALANCE] forKey:TABLE_BALANCE];
        [arrayMain addObject:secDict];
    }
    else
    {
        NSMutableDictionary *previousOBJ= [arrayMain objectAtIndex:[arrayMain count]-1];
        if ([[dict valueForKey:TABLE_TYPE] isEqualToString:@"Expense"])
        {
            [previousOBJ setValue:[NSString stringWithFormat:@"%d", [[previousOBJ valueForKey:TABLE_AMOUNT] integerValue] + [[dict valueForKey:TABLE_AMOUNT] integerValue]] forKey:TABLE_AMOUNT];
        }

        if ([[dict valueForKey:TABLE_TYPE] isEqualToString:@"Deposit"])
        {
            [previousOBJ setValue:[NSString stringWithFormat:@"%d", [[previousOBJ valueForKey:TABLE_NOTE] integerValue] + [[dict valueForKey:TABLE_AMOUNT] integerValue]] forKey:TABLE_NOTE];
        }

        [arrayMain replaceObjectAtIndex:[arrayMain count]-1 withObject:previousOBJ];
    }

    month = currentMonth;
    year = currentYear;
    if (i==source.count-1) {
        [lastDict setObject:year forKey:@"Year"];
        [lastDict setObject:arrayMain forKey:@"Data"];
        [lastArray addObject:lastDict];
    }

}
return lastArray;

}