NSMutableArray(inboxFaxItems)由以下字符串組成:MM/DD/YYYY hh:mm:ss a 例如: 2/2/2011 2:46:39 PM 2/4/2011 11:59 :47上午如何排序由日期/時間值字符串組成的NSMUtableArray?
我需要能夠排序此數組,以便最新的日期在頂部。
#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;
}
但是我得到的錯誤:
2011-06-29 02:49:28.782 app[4388:207] -[__NSDate isEqualToString:]: unrecognized selector sent 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
確定這似乎已經固定它:(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;
}
你在哪裏排序?在日期格式中使用'yyyy'而不是'YYYY'和'dd'代替'DD'。 'DD'表示年份的日期而不是月份。 「YYYY」並不總是與歷年相同。有關更多信息,請參見['this'](http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns)。 –
實際觸發錯誤的代碼似乎並未包含在內;這裏調用的方法都不應該導致isEqualToString:被調用;除了可能'[[self tableView] reloadData]',但沒有代碼來設置你的表視圖,很難說。 –
@Deepak日期我得到的是:2011/2/2 2:46:39 2/4/2011 11:59:47 AM。也請看看我問了幾個月回來的這個問題:http://stackoverflow.com/questions/5958998/how-to-sort-nsarray-with-date-time-values/5959393#5959393 – jini