在SegmentedControl更改上更改NSFetchedResultsController [英] Change NSFetchedResultsController on SegmentedControl change

查看:105
本文介绍了在SegmentedControl更改上更改NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView ,使用核心数据 NSFetchedResultsController 填充。我现在添加了一个 UISegmentedControl 到视图,当你更改当前段,我想更改tableview的内容。

I've got a UITableView which is filled using a Core Data NSFetchedResultsController. I've now added a UISegmentedControl to the view and when you change the current segment, I'd like the contents of the tableview to change.

我在网上看过,使用两个不同的 NSFetchedResultsControllers 是明智的,因为我可以从内置的缓存中受益。只有问题是我似乎找不到任何示例代码这样做,不知道从哪里开始。

I've read around online that it would be wise to use two different NSFetchedResultsControllers as then I can benefit from the built-in caching. Only problem is I can't seem to find any sample code for doing this and don't know where to begin.

任何人都可以解释从哪里开始创建第二 NSFetchedResultsController 并根据分段控件更改哪个源视图?

Can anyone explain where to begin in creating a second NSFetchedResultsController and changing which sources the tableview based on the segmented control?

查看头文件代码:

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

任何帮助都非常感谢。非常感谢。

Any help is much appreciated. Thanks.

UPDATE:已更新的代码

查看标头代码:

@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。当UISegmentedControl更新更新此ivar。

You could add another instance variable that represents the currently selected NSFetchedResultsController. And when the UISegmentedControl changes update this ivar.

这可能是由段的valuechange事件触发的操作

this could be the action that is triggered by the valuechange event of the segment

- (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方法作为示例:

one UITableViewDataSource method as example:

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

,您必须确保只有当前nsfrc触发tableview更新NSFetchedResultsControllerDelegate方法。所以你必须改变所有这些。

and you have to make sure that only the current nsfrc triggers a tableview update in the NSFetchedResultsControllerDelegate methods. So you have to change all of them too.

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






编辑:不,你做错了。 currentFetchedResultsController只是一个ivar,没有延迟加载getter。它是只是指向当前使用的控制器的指针。


Nope, you are doing it wrong. the currentFetchedResultsController is just an ivar, without a lazy loading getter. It's "just" a pointer to the currently used controller.

但是另外两个fetchedResultsController应该有一个延迟加载getter。

But the two other fetchedResultsControllers should have such a lazy loading 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;

这篇关于在SegmentedControl更改上更改NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆