如何对包含日期/时间值字符串的NSMUtableArray进行排序?

时间:2011-06-29 08:00:25

标签: objective-c ios

NSMutableArray(inboxFaxItems)由设置的字符串组成:MM / DD / YYYY hh:mm:ss a 例如: 2011年2月2日下午2:46:39 2/4/2011 11:59:47 AM

我需要能够对此数组进行排序,以便最新的日期位于顶部。

#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

确定这些东西已经固定了它:(更新)

// 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;
}

1 个答案:

答案 0 :(得分:2)

这是你的问题:

[[cell textLabel] setText:[dateArray objectAtIndex:[indexPath row]]];

setText:假设它得到一个字符串,因此,当决定是否更新自己时,它会向它传递的对象发送isEqualToString:方法,但 SURPRISE!< / strong>:这是一个NSDate对象。显然,哪个不响应那个选择器。

你需要做的是从你将它传递给标签之前的日期开始制作一个字符串,一切都会出现玫瑰。

- (NSString *)descriptionWithLocale:(id)locale

可能是实现这一目标的正确方法;但有可能

- (NSString *)description

将自动完成所有相关的语言环境。