0

在我的应用程序中,我将 coredata 中的日期作为字符串插入。如何按升序或降序获取这些日期。我使用下面的代码从 coredata 获取日期字符串并在节表中显示结果,并将获取的日期作为节标题。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(IBAction)insert:(id)sender
{
    NSManagedObjectContext *moc=[[self appDelegate] managedObjectContext];
    StringDates *entity=[NSEntityDescription insertNewObjectForEntityForName:@"StringDates" inManagedObjectContext:moc];
    entity.unused=insertTextField.text;
    entity.message=@"123456789";
    NSError *error;
    [moc save:&error];
    [insertTextField resignFirstResponder];

}
-(NSFetchedResultsController *)fetchedResultsController
{

    NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext];

    if (moc == nil) return nil;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"StringDates" inManagedObjectContext:moc];
    NSSortDescriptor    *sd1    = [[NSSortDescriptor alloc] initWithKey:@"unused" ascending:YES];

    NSArray *sortDescriptors    = [NSArray arrayWithObjects:sd1,nil];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    [fetchRequest setEntity:entity];
    [fetchRequest setSortDescriptors:sortDescriptors];

    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:moc sectionNameKeyPath:@"unused" cacheName:nil];

    [fetchedResultsController setDelegate:self];   

    NSError *error = nil;
    if (![fetchedResultsController performFetch:&error])
    {
        NSLog(@"Error performing fetch: %@", error);
    }


    return fetchedResultsController;



}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [stringDatesTableView reloadData];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{  

    return [[[self fetchedResultsController] sections] count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{  

    NSArray *sections = [[self fetchedResultsController] sections];

    if (section < [sections count])
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];

        return sectionInfo.numberOfObjects;
    }

    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *cellidenitifier=@"cell";


    StringDates *userRecentChat=[[self fetchedResultsController] objectAtIndexPath:indexPath];

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];

    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }

    cell.detailTextLabel.text=userRecentChat.message;
    return  cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    NSArray *sections = [[self fetchedResultsController] sections];

    if (section < [sections count])
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
        return sectionInfo.name;

    }
    return @"";
}

但是日期字符串既没有按顺序排列,也没有按顺序排列。谁能解决我的问题。谢谢。

4

2 回答 2

1

您可以只使用 NSDateFormatter 对象来创建 NSDate 对象并将它们存储到 CoreData 中。然后,您可以使用 NSSortdescriptor 轻松地对它们进行升序/降序排序。

只需快速查看 NSDateFormatter 参考 https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

和 DateFormattingGuide: https ://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html#//apple_ref/doc/uid/10000029i

这非常容易做到。

于 2013-05-03T10:27:41.063 回答
0

参考这个答案

compare:函数或块中使用从日期NSDateFormatter创建NSDate对象NSString,然后返回比较结果。比较结果将取决于您需要的排序顺序。

干杯!
阿马尔。

于 2013-05-03T12:21:55.160 回答