2011-04-16 29 views
5

我有一個UITableView,它使用核心數據NSFetchedResultsController填充。我現在在視圖中添加了UISegmentedControl,當您更改當前的細分時,我希望tableview的內容發生變化。更改SegmentedControl上的NSFetchedResultsController更改

我已經在網上閱讀過,明智地使用兩個不同的NSFetchedResultsControllers,因爲那時我可以從內置緩存中受益。唯一的問題是,我似乎無法找到任何示例代碼來做這件事,也不知道從哪裏開始。

任何人都可以解釋從哪裏開始創建第二個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; 

View實現代碼:

@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; 

View實現代碼:

@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; 
} 

回答

7

您可以添加一個表示當前選擇的NSFetchedResultsController另一個實例變量。當UISegmentedControl更新更新這個伊娃。

這可能是由段

- (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方法爲例的valuechange事件觸發的動作:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [[self.currentFetchedResultsController sections] count]; 
} 

,你必須確保只有當前nsfrc觸發NSFetchedResultsControllerDelegate方法中的tableview更新。所以你也必須改變他們所有人。

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    if (controller == self.currentFetchedResultsController) { 
     [self.tableView beginUpdates]; 
    } 
} 

編輯:不,你做錯了。 currentFetchedResultsController只是一個ivar,沒有延遲的加載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; 
+0

感謝您的答覆。我試圖按照上面提到的方式來完成它,但似乎並沒有在表格中顯示任何內容。我懷疑我在(NSFetchedResultsController *)fetchedResultsController方法中丟失了一些東西。我已經添加了我更新的代碼我原來的消息......不確定是否我打算使用__fetchedResultsController ... – 2011-04-17 11:58:33

+0

@Scrooby我希望我的編輯使得它一點點清晰。 – 2011-04-17 17:12:12

+0

輝煌,工作!我假設在你的代碼中,你有myKeyphraseFetchedResultsController它的意思是keyphraseFetchedResultsController?我做了這個改變,它似乎工作,所以謝謝! – 2011-04-17 18:08:27