I have the following array of data which I am fetching from SQLite3 database.
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;
}
)
I want to make a tableview with sections on yearly basis and in rows each section data will show monthly basis after performing calculation of income and expense. For more info I am attaching a screen shot herewith.
I am attempting to solve the problem using the following method.
-(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;
}