Hey guys,
I'm very new to iphone development, so you'll have to forgive me.
So currently I have a table that displays messages, but I'd like to group the messages by date, e.g.
Tuesday 4/5/98 //header
message 1
message 2
message 3
Wednesday 4/6/98
message 1
etc.
So right now, it's just one long NSMutableArray oldArr (sorted).
What I was thinking of doing, was creating another NSMutableArray (groupArr) of unique date objects (DateGroup), in which each DateGroup would have the following ivars:
int size; //number of messages for date
int index; //index of first message in the total array
//so I can easily retrieve the object when the section and row is asked
NSDate date; //need the date for the header
With these ivars, I can get all the sections with groupArr size, all the individual row sizes by accessing the DateGroup size, and the individual cell when given a section, row arguments by getting the index + row.
I think this is the best way to do it. However, I am having problems populating the groupArr from the oldArr (which will dynamically increase in size). I was thinking of going one by one through the oldArr with this psuedocode:
NSDate date = nil;
int size = 1;
int index = 0;
for (int i = 0; i < oldArr.size; i++) {
OldGroup* cur = [oldArr objectAt:i];
if (date is different from cur->date){ //i know, it's pseudocode
DateGroup* newGroup = [[DateGroup alloc] initWithDate:cur->date index:index];
[groupArr add:NewGroup];
date = cur->date;
index += size;
size = 1;
} else{ //the date is the same, so the object belongs in the group
[groupArr lastObject].size++;
}
}
I apologize for the messy code, I don't know how to format it in Stack Overflow.
Anyway, while I think this will work, it seems very unelegant to me. I was thinking about using the "indexOfObjectPassingTest" of NSMutableArry to find the next date, but can't seem to implement it conceptually. I'm trying to design a good way to do this. Any suggestions?
Thanks and I apologize for the confusing ideas here.