How to sort a NSMUtableArray consisting of strings of date/time values?
The NSMutableArray (inboxFaxItems)consists of strings set up like: MM/DD/YYYY hh:mm:ss a for example: 2/2/2011 2:46:39 PM 2/4/2011 11:59:47 AM
I need to be able to sort this array so that the newest dates are at the top.
#import <UIKit/UIKit.h>
@interface InboxTableViewController : UITableViewController<NSXMLParserDelegate> {
//all of these need to be put in custom class
NSMutableArray *inboxFaxItems;
NSMutableArray *dateArray;
NSMutableArray *inboxMessageID;
NSMutableArray *inboxMessagePageCount;
NSMutableArray *inboxMessageFrom;
NSMutableData *xmlData;
NSURLConnection *connectionInprogress;
NSMutableString *inboxFaxesString;
UIActivityIndicatorView *activityIndicator;
}
-(void) loadInbox;
@end
- (void)dealloc {
[inboxFaxItems release];
inboxFaxItems = nil;
[inboxMessageID release];
inboxMessageID = nil;
[inboxMessagePageCount release];
inboxMessagePageCount = nil;
[inboxMessageFrom release];
inboxMessageFrom = nil;
[dateArray release];
dateArray = nil;
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [dateArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"InboxFaxItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
[[cell textLabel]setText:[dateArray objectAtIndex:[indexPath row]]];
cell.imageView.image = [UIImage imageNamed:@"document.png"];
return cell;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];
[parser setDelegate:self];
[parser parse];
[parser release];
//do sorting here
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];
for (NSString *dateString in inboxFaxItems) {
NSDate *date = [formatter dateFromString:dateString];
if (date) [dateArray addObject:date];
// If the date is nil, the string wasn't a valid date.
// You could add some error reporting in that case.
}
[[self tableView] reloadData];
[connectionInprogress release];
connectionInprogress = nil;
[xmlData release];
xmlData = nil;
}
However I am getting errors:
2011-06-29 02:49:28.782 app[4388:207] -[__NSDate isEqualToString:]: unrecognized selector s开发者_如何学运维ent to instance 0x4d9e090
2011-06-29 02:49:28.784 app[4388:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate isEqualToString:]: unrecognized selector sent to instance
OK THIS SEEMS TO HAVE FIXED IT: (UPDATE)
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"InboxFaxItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateArray objectAtIndex:[indexPath row]];
NSString *dateStr = @"";
dateStr = [formatter stringFromDate:date];
[[cell textLabel]setText:dateStr];
cell.imageView.image = [UIImage imageNamed:@"document.png"];
return cell;
}
Here's your problem:
[[cell textLabel] setText:[dateArray objectAtIndex:[indexPath row]]];
setText:
assumes it gets a string, and as such, when deciding wether or not to update itself, it sends an isEqualToString:
method to the object it is passed, but SURPRISE!: It's an NSDate
object. Which doesn't respond to that selector, obviously.
What you need to do is to make a string from the date before you pass it to the label, and everything will come up roses.
- (NSString *)descriptionWithLocale:(id)locale
is probably the correct method for achieving this; but it's possible that
- (NSString *)description
will do all the relevant locale stuff automagically.
精彩评论