Core Data を使用する最新の SDK と XCode 4.5.2 で iOS アプリを開発しています。
には、と のUIViewController
2 つUITableView
がshopsList
ありproductsList
ます。Core Dataを使用したいのですが、使用するのはこれが初めてです。そこで、Xcode マスター ディテール テンプレートの使用を開始し、次のコードを見つけました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;
[ ... ]
shop
とがある場合product
NSManagedObject
。
- (NSFetchedResultsController *)fetchedResultsController
これら2つを使用するためにどのように適応できUITableView
ますか?
私はおそらくする必要がありNSFetchedResultsController
ますよね?