Tengo un UITableView
que se completa con un Core Data NSFetchedResultsController
. Ahora agregué un UISegmentedControl
a la vista y cuando cambias el segmento actual, me gustaría que el contenido de la tabla cambiara.Cambiar NSFetchedResultsController en SegmentedControl change
He leído en línea que sería aconsejable usar dos NSFetchedResultsControllers
diferentes, ya que puedo aprovechar el almacenamiento en caché integrado. El único problema es que parece que no puedo encontrar ningún código de muestra para hacer esto y no sé por dónde empezar.
¿Alguien puede explicar por dónde empezar creando un segundo NSFetchedResultsController
y cambiando las fuentes de la tabla basada en el control segmentado?
Ver código de cabecera:
#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;
Ver código de implementación:
@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;
}
Cualquier ayuda es muy apreciada. Gracias.
ACTUALIZACIÓN: código actualizado
Ver código de cabecera:
@property (nonatomic, retain) NSFetchedResultsController *currentFetchedResultsController;
@property (nonatomic, retain) NSFetchedResultsController *competitorFetchedResultsController;
@property (nonatomic, retain) NSFetchedResultsController *keyphraseFetchedResultsController;
Ver código de implementación:
@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;
}
Gracias por su respuesta. Intento hacerlo de la manera que mencionaste anteriormente, pero no parece mostrar nada en la tabla.Sospecho que me falta algo en el método (NSFetchedResultsController *) fetchedResultsController. He agregado mi código actualizado a mi mensaje original ... no estoy seguro si estoy destinado a usar __fetchedResultsController ... –
@Scrooby espero que mi edición lo aclare un poco. –
¡Brillante, eso funcionó! Supongo que en tu código de arriba donde tienes myKeyphraseFetchedResultsController está destinado a ser keyphraseFetchedResultsController? Hice ese cambio y parece funcionar así que ¡gracias! –