如何在IOS中以升序或降序从核心数据中获取字符串数据

时间:2013-05-03 10:19:26

标签: iphone core-data

在我的应用程序中,我在coredata中插入日期作为strings.how我可以按照降序或降序获取这些日期。我使用下面的代码从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 @"";
}

但日期字符串不是按顺序或降序排列。任何人都可以解决我的问题。谢谢你。

2 个答案:

答案 0 :(得分:1)

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

快速浏览一下NSDateFormatter Reference 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

这很容易做到。

答案 1 :(得分:0)

请参阅此answer

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

干杯!
阿玛尔。