我有一个具有开启和关闭模式的应用程序。在在线模式下,我正在检查服务器的登录、
下载 XML 文件并解析该文件。所有数据都写入 coreData。
我正在使用 NSFetchedResultsController 填充我的 TableView。
如果我使用关闭模式(要使用离线模式,coreData 实体不应该为零),
我只是关闭视图以显示带有 coreData 数据的 tableView。
这里的问题是:如果我使用离线模式,数据排序不正确。
reloadData 不起作用,我尝试了更多次。
我怎样才能重新加载 tableView,所以我也可以在关闭模式下正确排序数据?
编辑1:
在关闭模式下,我只这样做:
if (xmlFile) {
//FadeOut animation nach erfolgreichem Login
//und removeFromSuperview
[UIView animateWithDuration:0.8
animations:^{loginView.alpha = 0.0;}
completion:^(BOOL finished){ [loginView removeFromSuperview]; }];
}
如何对 viewDidAppear 上的 tableview 进行排序?
编辑 2:
这里是我的 NSFetchedResultsController:
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil)
{
return __fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntitySetsCards" inManagedObjectContext:self.managedObjectContext];
NSPredicate *inboxPred = [NSPredicate predicateWithFormat:@"archived == 0"];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
[fetchRequest setPredicate:inboxPred];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptorSetOrder = [[[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES] autorelease];
NSSortDescriptor *sortDescriptorColorOrder = [[[NSSortDescriptor alloc] initWithKey:@"colorOrder" ascending:YES] autorelease];
NSSortDescriptor *sortDescriptorSortOrder = [[[NSSortDescriptor alloc] initWithKey:@"sortingOrder" ascending:YES] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptorSetOrder, sortDescriptorSortOrder, sortDescriptorColorOrder, nil] autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
// nil for section name key path means "no sections".
// cacheName auf nil gesetzt, da @"Root" fehler erzeugt hat (FATAL ERROR)
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"setTitle" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
allFetchedCards = aFetchedResultsController;
allCards = [__fetchedResultsController fetchedObjects];
return __fetchedResultsController;
}