Reputation: 4122
I'll explain my issue by steps:
Now, I've used NSCalendar to find out in which week each Dictionary is going into and I've managed to dynamically find out how many rows (days/messages) in which sections (weeks) I have by this code:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSWeekCalendarUnit fromDate:[NSDate date]];
NSMutableArray *tempArray = [NSMutableArray new];
NSNumber *weekNumber; int countDictionary = 0;
if (!_sectionsDetailed)
_sectionsDetailed = [NSMutableArray new];
if (!_sections)
_sections = [NSMutableArray new];
if (!_sectionsDictionary)
_sectionsDictionary = [NSMutableDictionary new];
for (NSDictionary *dictionary in _historyArray)
{
++countDictionary;
dateComponents = [calendar components:NSWeekCalendarUnit fromDate:[dictionary objectForKey:@"messageDate"]];
weekNumber = [NSNumber numberWithInteger:[dateComponents week]];
[_sectionsDetailed addObject:weekNumber];
DLog(@"DICTIONARY NUMBER: %i - WEEK NUMBER: %@", countDictionary, weekNumber);
}
And from that I have the following output (I have 9 messages (rows/days) for 4 sections (weeks)):
DICTIONARY NUMBER: 1 - WEEK NUMBER: 5
DICTIONARY NUMBER: 2 - WEEK NUMBER: 5
DICTIONARY NUMBER: 3 - WEEK NUMBER: 5
DICTIONARY NUMBER: 4 - WEEK NUMBER: 3
DICTIONARY NUMBER: 5 - WEEK NUMBER: 3
DICTIONARY NUMBER: 6 - WEEK NUMBER: 2
DICTIONARY NUMBER: 7 - WEEK NUMBER: 2
DICTIONARY NUMBER: 8 - WEEK NUMBER: 1
DICTIONARY NUMBER: 9 - WEEK NUMBER: 1
Then, to have only a single instance of a week I count the sections (weeks) using NSBag:
NSBag *arrayCounter = [NSBag bag];
for (id section in _sectionsDetailed)
{
[arrayCounter add:section];
}
for (id uniqueSection in arrayCounter.objects)
{
[tempArray addObject:uniqueSection];
[_sectionsDictionary setObject:[NSNumber numberWithInteger:[arrayCounter occurrencesOf:uniqueSection]] forKey:uniqueSection];
DLog(@"WEEK:%@, MESSAGES/WEEK:%i", uniqueSection, [arrayCounter occurrencesOf:uniqueSection]);
}
And the output for the count is:
WEEK:5, MESSAGES/WEEK:3
WEEK:3, MESSAGES/WEEK:2
WEEK:1, MESSAGES/WEEK:2
WEEK:2, MESSAGES/WEEK:2
My question is: Since I need to populate the Sections and Rows of UITableView, how do I create a Nested NSArray or NSMutableArray to contain a scheme like this:
completeArray:
> Week 1 (section 1) [ARRAY]:
> Message 1 (row 1 / day 1 in week 1/section1) [DICT]
> Message 2 (row 2 / day 2 in week 1/section1) [DICT]
> Week 2 (section 2) [ARRAY]:
> Message 1 (row 1 / day 1 in week 2/section2) [DICT]
> Message 2 (row 2 / day 2 in week 2/section2) [DICT]
and so on... .
.
.
I've tried getting the result I need with the following code:
for (id section in _sections)
{
for (id dictionary in _historyArray)
{
dateComponents = [calendar components:NSWeekCalendarUnit fromDate:[dictionary objectForKey:@"messageDate"]];
weekNumber = [NSNumber numberWithInteger:[dateComponents week]];
if (section == weekNumber)
{
DLog(@"Section %@ - Dict %@",section, dictionary);
}
}
}
But I'm stuck at finding a solution to check which row goes to which section....
The output for the above is this:
Section 5 - Dict {
messageDate = "2013-02-01 11:06:41 +0000";
messageNumber = 50;
}
Section 5 - Dict {
messageDate = "2013-02-01 11:06:40 +0000";
messageNumber = 1;
}
Section 5 - Dict {
messageDate = "2013-02-01 11:06:39 +0000";
messageNumber = 52;
}
Section 3 - Dict {
messageDate = "2013-01-18 09:18:47 +0000";
messageNumber = 88;
}
Section 3 - Dict {
messageDate = "2013-01-18 09:18:46 +0000";
messageNumber = 58;
}
Section 2 - Dict {
messageDate = "2013-01-11 09:18:24 +0000";
messageNumber = 45;
}
Section 2 - Dict {
messageDate = "2013-01-11 09:18:06 +0000";
messageNumber = 35;
}
Section 1 - Dict {
messageDate = "2013-01-04 09:17:40 +0000";
messageNumber = 30;
}
Section 1 - Dict {
messageDate = "2013-01-04 08:16:40 +0000";
messageNumber = 53;
}
Please help me out if you can. I will really appreciate your help! Thank you!
Upvotes: 1
Views: 246
Reputation: 4122
Sorry for the late answer (I didn't had the time), I've come up with the following solution: Thanks for the help Jeremy! It was done with your help.
- (void) archiveRecord
{
NSDate *currentDate = [NSDate date];
NSNumber *currentMessageNumber = [NSNumber numberWithInt:_generatedNumber];
NSDictionary *currentMessageDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:currentMessageNumber,@"messageNumber", currentDate,@"messageDate", nil];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentMessageDateComponents;
NSDateComponents *historyMessageDateComponents;
currentWeekNumber = 0;
historyWeekNumber = 0;
currentMessageDateComponents = [calendar components:NSWeekCalendarUnit fromDate:currentDate];
currentWeekNumber = [currentMessageDateComponents week];
if (historyExists)
{
for (NSMutableArray *sectionArray in [_messageHistory objectAtIndex:0])
{
DLog(@"SECTIONARRAY: %@", sectionArray);
DLog(@"DICTIONARY NUMBER: %@", [sectionArray valueForKey:@"messageNumber"]);
historyMessageDateComponents = [calendar components:NSWeekCalendarUnit fromDate:[sectionArray valueForKey:@"messageDate"]];
historyWeekNumber = [historyMessageDateComponents week];
if (historyWeekNumber == currentWeekNumber)
{
DLog(@"ADD MESSAGE TO CURRENT WEEK");
[[_messageHistory objectAtIndex:0] insertObject:currentMessageDictionary atIndex:0];
break;
}
else
{
DLog(@"NEW SECTION NEEDED: CREATE NEW SECTION AND ADD MESSAGE");
[_messageHistory insertObject:[NSMutableArray arrayWithObjects:currentMessageDictionary, nil] atIndex:0];
break;
}
}
}
else
{
DLog(@"CREATE NEW SECTION AND ADD MESSAGE");
[_messageHistory addObject:[NSMutableArray arrayWithObjects:currentMessageDictionary, nil]];
}
DLog(@"CURRENT HISTORY: %@", _messageHistory);
[self performSelector:@selector(writeRecordToFile)];
}
Upvotes: 0
Reputation: 1029
Ok, so the way you have the data structures right now makes what you want to do pretty much impossible. In fact, there are a lot of easier ways to do the things being done here.
So instead of keeping all these dictionaries (which really should be made into a message class if we are being picky) in an array, try making an NSDictionary along of the lines of the key being "Week #" and the value being an array of the messages (or in this case dictionaries).
So then to populate the TableView, numberOfSections = [[aDictionary allKeys] count]... then enumerate for the sections. And numberOfRows for each section = [[aDictionary objectForKey: aSection] count], this gives you the array of messages for the week and then you can just enumerate on the array. (hope the point got across... pretty sloppy on my part sorry)
The problem you are running in to is one that can easily be avoided by using the right kind of data structures, the TableView is a two dimensional array and you are trying to fill each element of it singly instead of matching the data structure which just makes a big headache, as you're probably aware of haha
Upvotes: 1