I have an events app which gets events data from sqlite database and displays it in tableView. I also converted date from the sqlite table from NSString to NSDate. Now I want to display records sorted by Date and also don't want to display event that is finished.
Here is my EventViewController.m
#import "EventViewController.h"
@implementation EventViewController
@synthesize events,eDate;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Events";
//Get the DBAccess object;
DBAccess *dbAccess = [[DBAccess alloc] init];
//Get event array from database
self.events = [dbAccess getAllEvents];
//Close the database
[dbAccess closeDatabase];
//Release dbAccess object to free its memory
[dbAccess release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return YES;
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"# of Sections");
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//NSLog(@"Countring Rows");
return [self.events count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Creating cell");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
开发者_高级运维 {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
event = [self.events objectAtIndex:[indexPath row]];
// Configure the cell.
cell.textLabel.text = event.name;
cell.detailTextLabel.text = event.location;
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Formate Date from string to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSDate *eeDate = [dateFormatter dateFromString:event.date];
NSLog(@"%@", eeDate);
[dateFormatter setDateFormat:@"EEEE MMMM d, YYYY"];
eDate = [dateFormatter stringFromDate:eeDate];
NSLog(@"%@", eDate);
[dateFormatter release];
//Event *event;
if (tableView == self.tableView)
{
event = [self.events objectAtIndex:[indexPath row]];
//event = [[self.events objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
}
else
{
event = [self.events objectAtIndex:[indexPath row]];
}
EventDetailViewController *detailViewController = [[EventDetailViewController alloc]
initWithStyle:UITableViewStyleGrouped];
detailViewController.eventDetail = [events objectAtIndex:indexPath.row];
detailViewController.eventName = event.name;
detailViewController.eventLocation = event.location;
detailViewController.eventNote = event.note;
detailViewController.eventDate = eDate;
detailViewController.eventTime = event.time;
//Pust detail controller on to the stack
[self.navigationController pushViewController:(UITableViewController *)detailViewController animated:YES];
//release the view controller
[detailViewController release];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc
{
[events release];
[super dealloc];
}
@end
Please Help.
Since the date is represented as yyyyMMdd
, we can use the usual string comparison to eliminate older events using the current date. Then we can sort the remaining events.
NSArray * allEvents = [dbAccess getAllEvents];
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSString * todaysDateAsString = [dateFormatter stringFromDate:[NSDate date]];
NSArray * futureEvents = [allEvents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"date >= %@", todaysDateAsString]];
NSSortDescriptor * descriptor = [NSSortDescriptor descriptorWithKey:@"date" ascending:YES];
self.events = [futureEvents sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
You can use NSSortDescriptors to sort your events.
Suppose you have a property finished
that indicates when the event is finished, you could filter the array like this
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"finished = NO"];
self.events = [[dbAccess getAllEvents] filteredArrayUsingPredicate:predicate];
And than, if the event object has a date
property that is the one you are trying to sort ascending, you could use NSSortDescriptor
to sort it.
NSSortDescriptor *descriptor = [NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *arrayDescriptors = [NSArray arrayWithObject: descriptor];
self.events = [self.events sortedArrayUsingDescriptors:arrayDescriptors];
[descriptor release];
精彩评论