Sort UITableview in objective c with dates descending NSDictionary
Thank you for looking into my question here. I am trying to get uitableview sectioned by dates in descending order. I get the data from the sqllite order by datefield desc. But whatever I do the dates are displayed in ascending order. I have the following set of data that comes out of the db and in this order:
ID BookName DateRead
1 ABC 19-10-2011
2 ABZ 27-06-2011
3 ABD 28-05-2011
I would like the data to appear like the following
19-10-2011
ABC
27-06-2011
ABZ
28-05-2011
ABD
but no matter what I am trying I am getting the data returned as below:
19-10-2011 ABC 28-05-2011 ABZ 27-06-2011 ABDHere is the complete list of code that I am using: .h file
#import <UIKit/UIKit.h>
@interface BookHistoryViewController : UITableViewController {
NSArray *books;
NSMutableDictionary *sections;
}
@property (nonatomic,retain) NSArray *books;
@property (nonatomic,retain) NSMutableDictionary *sections;
@end
Here is my .m file
- (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 (cel开发者_开发知识库l == 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;
}
When you get the book in tableView:cellForRowAtIndexPath:
, you first sort by section key which is the dateRead as a string, but you sort in ascending order. You probably want to sort using a descriptor like you do in viewDidLoad
. In fact, why not just keep it in the correctly sorted order so that you don't have to sort each time you need a cell? The way you are doing it will cause the interface to grind to a halt pretty quickly.
精彩评论