2011-06-28 49 views
1

感謝您在這裏查看我的問題。我正在嘗試按降序排列按日期排列的uitableview。我通過datefield desc獲取sqllite命令的數據。但無論我做什麼,日期都按升序顯示。我有下面的一組數據的出來的分貝和順序:在日期降序NSDictionary中排序目標c中的UITableview

ID BookName DateRead 
1 ABC  19-10-2011 
2 ABZ  27-06-2011 
3 ABD  28-05-2011 

我想數據出現類似下面的

19-10-2011 
ABC 
27-06-2011 
ABZ 
28-05-2011 
ABD 

但不管如何我想我我得到返回的數據如下:
2011-10-19
ABC
28-05-2011
ABZ
27-06-2011
ABD

這裏是代碼的完整列表,我現在用: .h文件中

#import <UIKit/UIKit.h> 
    @interface BookHistoryViewController : UITableViewController { 
     NSArray *books; 
     NSMutableDictionary *sections; 
    } 
    @property (nonatomic,retain) NSArray *books; 
    @property (nonatomic,retain) NSMutableDictionary *sections; 

@end 

這裏是我的.m文件

- (void)viewDidLoad { 

    TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate]; 


    [Book getInitialDataToDisplay:[appDelegate getDBPath]]; 



    self.books = [NSMutableArray arrayWithArray:appDelegate.bookArray]; 


    self.sections = [[NSMutableDictionary alloc] init]; 



    BOOL found; 

    // Loop through the books and create our keys 

    for (NSDictionary *book in books) 
    {  


     NSString *c = [book valueForKey:@"DateRead"]; 

     found = NO; 

     for (NSString *str in [self.sections allKeys]) 
     { 
      if ([str isEqualToString:c]) 
      { 
       found = YES; 
      } 
     } 

     if (!found) 
     {  
      [self.sections setValue:[[NSMutableArray alloc] init] forKey:c]; 
     } 
    } 

    // Loop again and sort the books into their respective keys 
    for (NSDictionary *book in self.books) 
    { 
     [[self.sections valueForKey:[book valueForKey:@"DateRead"]] addObject:book]; 
    }  

    // Sort each section array 
    for (NSString *key in [self.sections allKeys]) 
    { 
     [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"DateRead" ascending:NO]]]; 
    }  

    [super viewDidLoad]; 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[self.sections allKeys] count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{  
    return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:section]; 


} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:section]] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSDictionary *book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [book valueForKey:@"title"];  


    return cell; 


} 

回答

0

當您在tableView:cellForRowAtIndexPath:拿到書,您首先按照段落關鍵字進行排序,該關鍵字是dateRead作爲字符串,但按升序排序。您可能想要使用viewDidLoad中的描述符進行排序。事實上,爲什麼不把它保持在正確的排序順序,以便每次你需要一個單元格時不需要排序呢?你這樣做的方式會導致界面很快崩潰。

+0

謝謝Jeremy的回覆。我已經從tableView:cellForRowAtIndexPath中進行了排序:但仍然按升序顯示日期。 – snowflakes74

+0

您仍然需要排序,因爲NSDictionary不會對其鍵進行排序,但是您需要使用描述符進行排序,就像在viewDidLoad中做的那樣 – JeremyP