如何在tableView中显示按NSDate排序的事件?

发布于 2024-11-16 06:52:57 字数 4306 浏览 2 评论 0原文

我有一个事件应用程序,它从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2024-11-23 06:52:57

由于日期表示为 yyyyMMdd,我们可以使用通常的字符串比较来消除使用当前日期的旧事件。然后我们可以对剩余的事件进行排序。

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]];

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]];
橘虞初梦 2024-11-23 06:52:57

您可以使用 NSSortDescriptors 对事件进行排序。

假设您有一个属性 finished 指示事件何时完成,您可以像这样过滤数组

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"finished = NO"];
self.events = [[dbAccess getAllEvents] filteredArrayUsingPredicate:predicate];

,并且如果事件对象有一个 date 属性,则该属性就是该属性如果您尝试按升序排序,可以使用 NSSortDescriptor 对其进行排序。

NSSortDescriptor *descriptor = [NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *arrayDescriptors = [NSArray arrayWithObject: descriptor];
self.events = [self.events sortedArrayUsingDescriptors:arrayDescriptors];
[descriptor release];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文