2011-06-29 19 views
1

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; 
} 
+2

你在哪裏排序?在日期格式中使用'yyyy'而不是'YYYY'和'dd'代替'DD'。 'DD'表示年份的日期而不是月份。 「YYYY」並不總是與歷年相同。有關更多信息,請參見['this'](http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns)。 –

+0

實際觸發錯誤的代碼似乎並未包含在內;這裏調用的方法都不應該導致isEqualToString:被調用;除了可能'[[self tableView] reloadData]',但沒有代碼來設置你的表視圖,很難說。 –

+0

@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

回答

2

這是你的問題:

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

setText:假定它得到了一塊土地ng,因此,當決定是否更新自身時,它會向其傳遞的對象發送isEqualToString:方法,但是驚奇!:這是一個NSDate對象。顯然,這對選擇器沒有反應。

你需要做的是從你傳遞給標籤之前的日期開始創建一個字符串,並且所有東西都會出現在玫瑰花上。

- (NSString *)descriptionWithLocale:(id)locale 

可能是實現此目的的正確方法;但它可能是

- (NSString *)description 

將自動執行所有相關的區域設置的東西。

+0

你是對的。我已經更新了我的代碼,事情似乎沒有問題。如果你可以看看我更新的方法。自從日期來自服務器以來,我並不太在意區域設置。 – jini

+0

@jini:看起來是正確的,但是如果除了顯示日期之外你沒有對日期做任何事情;我建議移動到更加健全的日期格式,如YYYY-MM-DDTHH:MM:SS(ISO格式)。這將允許簡單的文本排序,解決了一些頭痛問題。 –

+0

我會把它放在客戶面前,因爲它們是可用的Web服務。再次感謝 – jini

相關問題