一个UIViewController中的两个UITableViews的一个FetchedResultsController

时间:2013-01-23 08:52:46

标签: ios uitableview core-data

我正在使用最新的SDK和使用Core Data的XCode 4.5.2开发iOS应用程序。

UIViewController我将有两个UITableViewshopsListproductsList。我想使用核心数据,这是我第一次使用它。所以,我已经开始使用Xcode Master Detail模板,我在MasterViewController.m上找到了这段代码:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" 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:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [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:@"Master"];
    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. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

如果我有这个UIViewController界面:

@interface FirstViewController : UIViewController<CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
{
    CLLocationManager* locationManager;
    BOOL isMenuHidden;
    BOOL isShopsListOpen;
    BOOL isProductsListOpen;
    long int selectedShopRow;
    long int selectedProductRow;
    NSIndexPath* shopCheckedIndexPath;
    NSIndexPath* productCheckedIndexPath;
}

@property (unsafe_unretained, nonatomic) IBOutlet UITableView *shopsList;
@property (unsafe_unretained, nonatomic) IBOutlet UITableView *productsList;

[ ... ]

如果我有shopproduct NSManagedObject

如何调整- (NSFetchedResultsController *)fetchedResultsController以使用这两个UITableView

我可能需要NSFetchedResultsController,不是吗?

2 个答案:

答案 0 :(得分:3)

首先在app delegate中分配viewController的managedObjectContext。

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *productsListFetchedResultsController;
@property (strong, nonatomic) NSFetchedResultsController *shopsListFetchedResultsController;

在数据模型中创建2个实体,并在需要时填写appdelegate中的一些可用数据。

在viewWillAppear中,您将2个实体提取到2个NSFetchedResultsController,只需在实用工具面板中拖动基本的fetchResult块代码,并将上下文更改为self.manageObjectContext,将实体名称更改为关联名称

答案 1 :(得分:0)

解决方案是使用NSFetchedResultsController的两个实例。我的一个应用程序中有类似的设置。

我建议将获取的结果控制器定义为视图控制器的@property

重要:不要忘记检查FRC委托回调中调用的控制器,例如controller:didChangeObject:...