2011-06-28 10 views
1

ここで質問してくれてありがとうございます。私はuitableviewを降順で日付で区切るようにしようとしています。私は、日付フィールドのdescでsqlliteの順序からデータを取得します。しかし、私が何をしても、日付は昇順に表示されます。UITableviewを目的語cで降順に並べ替えます。NSDictionary

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 

が、私は私をしようとしていますどのように関係なく、のように見えるしたいと思います:私は、DBのうち、この順に来るデータの次のセットを持っていますデータは以下のように返さ取得しています:
19-10-2011
を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 

はあなたがtableView:cellForRowAtIndexPath:で書籍を取得するとここに私の.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

あり、最初にセクションのキーで並べ替えます。これは文字列としてdateReadですが、昇順で並べ替えます。 viewDidLoadのように、ディスクリプタを使用してソートすることをお勧めします。実際には、セルを必要とするたびにソートする必要がないように、正しくソートされた順序に保つだけではいかがですか?あなたがそれをやっているやり方は、インターフェイスがかなり速く停止するようにします。

+0

返信いただいたJeremyありがとうございます。私はtableView:cellForRowAtIndexPathからソートを行いましたが、まだ昇順で日付が表示されています。 – snowflakes74

+0

NSDictionaryはキーを並べ替えないので、ソートが必要ですが、viewDidLoadのようにソートする必要があります – JeremyP

関連する問題