更改SegmentedControl上的NSFetchedResultsController更改
我有一个使用核心数据NSFetchedResultsController
填充的UITableView
。 我现在已经添加了一个UISegmentedControl
到视图,当你改变当前的段,我希望tableview的内容改变。
我已经在网上阅读,使用两个不同的NSFetchedResultsControllers
是明智的,因为我可以从内置caching中受益。 唯一的问题是我似乎无法find任何示例代码做这个,不知道从哪里开始。
任何人都可以解释从哪里开始创build第二个NSFetchedResultsController
并更改基于分段控件的tableview的来源?
查看标题代码:
#import <CoreData/CoreData.h> @interface DomainViewController : UIViewController <NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate> { UITableView *domainView; UISegmentedControl *segmentedControl; NSString *domain; } @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController; @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain) NSString *domain; @property (nonatomic, retain) IBOutlet UITableView *domainView; @property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl; - (IBAction)segmentedControlIndexChanged;
查看执行代码:
@interface DomainViewController () - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; @end @implementation DomainViewController @synthesize fetchedResultsController = __fetchedResultsController; @synthesize managedObjectContext = __managedObjectContext; @synthesize domain; @synthesize domainView; @synthesize segmentedControl; - (void)viewDidLoad { [super viewDidLoad]; if (self.managedObjectContext == nil) { self.managedObjectContext = [(GARankingsAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; } } - (IBAction)segmentedControlIndexChanged { switch(self.segmentedControl.selectedSegmentIndex){ case 0: break; case 1: break; default: break; } } - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; cell.textLabel.text = [[managedObject valueForKey:@"Keyphrase"] description]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.fetchedResultsController sections] count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo numberOfObjects]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. [self configureCell:cell atIndexPath:indexPath]; return cell; } - (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]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Result" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Keyphrase" ascending:NO]; 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:@"Root"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; [aFetchedResultsController release]; [fetchRequest release]; [sortDescriptor release]; [sortDescriptors release]; 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(); } return __fetchedResultsController; }
任何帮助深表感谢。 谢谢。
更新:更新的代码
查看标题代码:
@property (nonatomic, retain) NSFetchedResultsController *currentFetchedResultsController; @property (nonatomic, retain) NSFetchedResultsController *competitorFetchedResultsController; @property (nonatomic, retain) NSFetchedResultsController *keyphraseFetchedResultsController;
查看执行代码:
@synthesize currentFetchedResultsController = __fetchedResultsController; @synthesize competitorFetchedResultsController; @synthesize keyphraseFetchedResultsController; - (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]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Result" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Keyphrase" ascending:NO]; 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:@"Root"]; aFetchedResultsController.delegate = self; //self.fetchedResultsController = aFetchedResultsController; self.currentFetchedResultsController = aFetchedResultsController; [aFetchedResultsController release]; [fetchRequest release]; [sortDescriptor release]; [sortDescriptors release]; NSError *error = nil; //if (![self.fetchedResultsController performFetch:&error]) if (![self.currentFetchedResultsController 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(); } return __fetchedResultsController; }
您可以添加另一个表示当前选定的NSFetchedResultsController的实例variables。 而当UISegmentedControl更改更新这个伊娃。
这可能是由段的valuechange事件触发的操作
- (IBAction *)segmentChanged:(UISegmentedControl *)sender { if ([sender selectedSegmentIndex] == 0) { self.currentFetchedResultsController = self.nsfrc1; [self.tableView reloadData]; } else if ([sender selectedSegmentIndex] == 1) { self.currentFetchedResultsController = self.nsfrc2; [self.tableView reloadData]; } }
以一个UITableViewDataSource方法为例:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.currentFetchedResultsController sections] count]; }
你必须确保只有当前的nsfrc触发NSFetchedResultsControllerDelegate方法中的tableview更新。 所以你也必须改变他们。
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { if (controller == self.currentFetchedResultsController) { [self.tableView beginUpdates]; } }
编辑:不,你做错了。 currentFetchedResultsController只是一个伊娃,没有一个懒惰的加载getter。 这只是一个指向当前使用的控制器的指针。
但另外两个fetchedResultsControllers应该有这样一个懒加载getter。
- (NSFetchedResultsController *)competitorFetchedResultsController { if (!myCompetitorFetchedResultsController) { // create competitorFetchedResultsController } return myCompetitorFetchedResultsController; } - (NSFetchedResultsController *)keyphraseFetchedResultsController { if (!myKeyphraseFetchedResultsController) { // create keyphraseFetchedResultsController } return myKeyphraseFetchedResultsController; }
然后切换:
self.currentFetchedResultsController = self.keyphraseFetchedResultsController;
要么
self.currentFetchedResultsController = self.competitorFetchedResultsController;