gpt4 book ai didi

ios - 一个 FetchedResultsController 用于一个 UIViewController 中的两个 UITableView

转载 作者:行者123 更新时间:2023-11-28 20:24:31 25 4
gpt4 key购买 nike

我正在使用最新的 SDK 和使用核心数据的 XCode 4.5.2 开发一个 iOS 应用。

UIViewController 上,我将有两个 UITableViewshopsListproductsList。我想使用 Core Data,这是我第一次使用它。所以,我开始使用 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):

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

[ ... ]

如果我有一个 shop 和一个 product NSManagedObject

我怎样才能使 - (NSFetchedResultsController *)fetchedResultsController 适应这两个 UITableView

我可能需要 NSFetchedResultsController,不是吗?

最佳答案

首先在应用委托(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 block 代码拖到实用程序面板中并将上下文更改为 self.manageObjectContext,将实体名称更改为关联名称

关于ios - 一个 FetchedResultsController 用于一个 UIViewController 中的两个 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14475652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com