Merci d'avoir regardé ma question ici. J'essaie de voir les résultats en ordre décroissant. Je reçois les données de la commande sqllite par datefield desc. Mais quoi que je fasse, les dates sont affichées dans l'ordre croissant. J'ai la série suivante de données qui sort de la db et dans cet ordre:Trier UITableview dans l'objectif c avec les dates descendant NSDictionary
ID BookName DateRead
1 ABC 19-10-2011
2 ABZ 27-06-2011
3 ABD 28-05-2011
Je voudrais que les données apparaissent comme les suivantes
19-10-2011
ABC
27-06-2011
ABZ
28-05-2011
ABD
mais peu importe ce que je suis en train I je reçois les données retournées comme ci-dessous:
19-10-2011
ABC
28-05-2011
ABZ
27-06-2011
ABD
Voici la liste complète des codes que j'utilise: .h
#import <UIKit/UIKit.h>
@interface BookHistoryViewController : UITableViewController {
NSArray *books;
NSMutableDictionary *sections;
}
@property (nonatomic,retain) NSArray *books;
@property (nonatomic,retain) NSMutableDictionary *sections;
@end
Voici mon fichier .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;
}
Merci Jeremy pour votre réponse.J'ai pris le tri de la tableView: cellForRowAtIndexPath: mais il montre toujours les dates dans l'ordre croissant. – snowflakes74
Vous avez toujours besoin du tri car NSDictionary n'ordonne pas ses clés, mais vous devez trier avec un descripteur comme dans viewDidLoad – JeremyP