我有由fetched results controller管理的表视图。 我想要的是,当用户点击分段控件时更改排序顺序。 这就是我创建获取结果控制器的方法:
- (NSFetchedResultsController *)frc {
if (_frc != nil) {
return _frc;
}
NSString *sortCase;
switch (self.sortOrder) {
case tableSortServer:
sortCase = [NSString stringWithFormat:@"%@", CD_SORT];
break;
case tableSortDate:
sortCase = [NSString stringWithFormat:@"%@", CD_DATE];
break;
default:
sortCase = [NSString stringWithFormat:@"%@", CD_SORT];
break;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:CD_ENTITY_NAME inManagedObjectContext:self.getManagedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:sortCase ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:BATCH_SIZE];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.getManagedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
self.frc = theFetchedResultsController;
_frc.delegate = self;
return _frc;
}
在分段控制器回调方法中,我做了:
- (IBAction)sortOrderChanged:(id)sender {
UISegmentedControl *sc = sender;
if (sc.selectedSegmentIndex == 0){
self.sortOrder = tableSortServer;
[self.tableView reloadData];
} else {
self.sortOrder = tableSortDate;
[self.tableView reloadData];
}
}
然而,它不起作用。如何实现这一任务? (重新加载具有不同排序顺序的表)?
答案 0 :(得分:1)
也许这是因为
if (_frc != nil) {
return _frc;
}
你重装表。它使用旧的获取结果控制器。因此,您的self.sortOrder = tableSortServer
不会被考虑在内。我认为每次重新加载表视图之前都需要设置新的排序描述符(并实际执行获取)