当我向UITableView添加单元格时,它们按字母顺序排序。当我重新加载视图时,它们按timeStamp排序。我希望它们按时间顺序按时间排序,而不是按字母顺序排序。
这是我添加单元格的地方:
-(IBAction)createSet
{
Set *set = (Set *)[NSEntityDescription insertNewObjectForEntityForName:@"Set" inManagedObjectContext:managedObjectContext];
set.weight = [NSNumber numberWithFloat:weightSelected2];
set.reps = [NSNumber numberWithInt:repSelected];
set.timeStamp = self.picker.date;
[self.exercise addSetsObject:set];
NSError *error = nil;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
NSLog(@"error: %@", error);
[setsTableView reloadData];
}
这是我的fetchedResultsController:
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController != nil)
{
return fetchedResultsController;
}
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Set" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"sets == %@", self.exercise]];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return self.fetchedResultsController;
}
答案 0 :(得分:0)
尝试在添加单元格后重新加载表格。