(Prepare to witness a newbie being very, very confused at what I'd assume is a basic form of logic that my brain is struggling to grasp.)
I have a .plist file at the moment. In the "Key" column there are names of events (it's just dummy content at the moment), and in the "Value" column there are dates in this format: 19-07-2012. Each row is of the "string" type.
In the viewDidLoad method, I use the following code:
NSString *theFile = [[NSBundle mainBundle] pathForResource:@"dates" ofType:@"plist"];
theDates = [[NSDictionary alloc] initWithContentsOfFile:theFile];
theDatesList = [theDates allKeys];
This loads the plist file into the dictionary, then I load the keys into an array, which is the way I've learned to populate a UITableView
, specifically with this code in the cellForRowAtIndexPath
method:
NSString *eventFromFile = [theDatesList objectAtIndex:indexPath.row];
NSString *dateFromFile = [theDates objectForKey:[theDatesList objectAtIndex:indexPath.row]];
But what I'm confused about now is, how do I order the cells of the UITableView
based on what dates are the soonest? So, the cell for the 19th of July would appear before the 21st of August, no matter what order it's in within the plist file.
Within the UITableViewCell
I've managed to calculate the number of days between the current date and the date defined within the plist. That's this code:
// Set the date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
// Get the current, then future date
NSDate *currentDate = [NSDate date];
NSDate *futureDate = [dateFormatter dateFromString:dateFromFile];
// Create the calendar object
NSCalendar *theCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Extract the "day" component from the calendar object
NSDateComponents *theDifferenceBetweenDays = [theCalendar components:NSDayCalendarUnit
fromDate:currentDate
toDate:futureDate
options:0];
NSInteger theRemainingDays = [theDifferenceBetweenDays day];
But I really have no idea what I'm doing. Could someone give me a nudge in the right direction? I've looked into NSSortDescriptors
and the sortedArrayUsingSelector
method, which seem to be relevant, but the act of actual implementation has left me stuck for the past six hours. Or maybe they're not the right track. Like I said, I'm quite confused.
Thanks.