How to sort my UITableViewCells based on dates in a property list?

(Get ready to testify that the newbie is very, very confused by what I suppose to be the main form of logic that my brain is trying to understand).

At the moment I have a .plist file. In the β€œKey” column there are event names (this is just fictitious content at the moment), and in the β€œValue” column there are dates in this format: 07/19/2012. Each line is of type "string".

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 that I found out to populate the UITableView , especially with this code in the cellForRowAtIndexPath method:

 NSString *eventFromFile = [theDatesList objectAtIndex:indexPath.row]; NSString *dateFromFile = [theDates objectForKey:[theDatesList objectAtIndex:indexPath.row]]; 

But now I'm confused about how I can arrange UITableView cells depending on which dates are fastest? So, the cell for July 19th will appear before August 21st, regardless of the order in which it is inside the plist file.

In UITableViewCell I was able to calculate the number of days between the current date and the date defined within the plist. What is 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 don't know what I'm doing. Can someone give me a push in the right direction? I looked at the NSSortDescriptors method and sortedArrayUsingSelector , which seems to matter, but the effect of the actual implementation has left me stuck for the last six hours. Or maybe this is the wrong way. As I said, I am very confused.

Thanks.

+4
source share
2 answers

There are several ways to sort NSArray NSDates in descending order.

You can use sortedArrayUsingDescriptors: ::

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO]; theDatesList = [theDatesList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

But I personally prefer sortedArrayUsingComparator: ::

 theDatesList = [theDatesList sortedArrayUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2){ return [date2 compare:date1]; }]; 
+1
source

There is a C-equivariant object for monotouch.dialog. with this, it should be very easy.

-2
source

Source: https://habr.com/ru/post/1413381/


All Articles