如何在tableView中显示按NSDate排序的事件?
我有一个事件应用程序,它从 sqlite 数据库获取事件数据并将其显示在 tableView 中。我还将 sqlite 表中的日期从 NSString 转换为 NSDate。 现在我想显示按日期排序的记录,并且不想显示已完成的事件。
这是我的 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
请帮忙。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于日期表示为 yyyyMMdd,我们可以使用通常的字符串比较来消除使用当前日期的旧事件。然后我们可以对剩余的事件进行排序。
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.您可以使用 NSSortDescriptors 对事件进行排序。
假设您有一个属性
finished
指示事件何时完成,您可以像这样过滤数组,并且如果事件对象有一个
date
属性,则该属性就是该属性如果您尝试按升序排序,可以使用NSSortDescriptor
对其进行排序。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 thisAnd than, if the event object has a
date
property that is the one you are trying to sort ascending, you could useNSSortDescriptor
to sort it.