获取关系并在UITableView中显示 [英] Fetch relationships and show in a UITableView
问题描述
我的核心数据模型在我的购物车应用中设置了以下关系。
I have the following relationship set up in my Core Data model, in my shopping cart app.
菜单
- >> 产品
<< - 购物车
(见下图)。
Menu
->> Product
<<- Cart
(See picture below).
以及具有以下代码的Objective-C类别:
And a Objective-C category with the following code:
+ (Cart *)addProductToCartWithProduct:(Product *)product inManagedObjectContext:(NSManagedObjectContext *)context
{
Cart *cart = [NSEntityDescription insertNewObjectForEntityForName:@"Cart" inManagedObjectContext:context];
NSManagedObjectID *retID = [product objectID];
[(Product *)[context objectWithID:retID] setInCart:cart];
[cart addProductsObject:(Product *)[context objectWithID:retID]];
return cart;
}
当用户按下应用中的添加到购物车按钮时,方法被触发(上面的方法)。我现在想要获取这些产品(由用户添加到购物车中),在我的其他类中,购物车所在的位置。我希望这些产品显示在该类的 UITableView
中,因此我使用了FRC。 但我无法弄清楚如何获取关系,从而显示产品。我将如何设法做到这一点?
When the user pushes the "add to cart" button in the app, this method is fired (the method above). I now want to fetch these products (which is added to the cart by the user), in my other class, where the "cart" is located. I want these products to be shown in my UITableView
in that class, I therefore use an FRC. But I can't figure out how to fetch the relationship and thereby show the products. How would I manage to do this?
我现在有以下代码。
- (void)loadCart
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"Cart" inManagedObjectContext:_theManagedObjectContext];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];
fetchRequest.sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[NSFetchedResultsController deleteCacheWithName:@"cartproducts"];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_theManagedObjectContext sectionNameKeyPath:nil cacheName:@"cartproducts"];
_fetchedResultsController.delegate = self;
NSError *error = nil;
if (![_fetchedResultsController performFetch:&error]) {
NSLog(@"Fetch Failed");
}
}
我的 UITableview
cellForRowAtIndexPath:
方法。
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *plainCellIdentifier = @"OrderCell";
ProductsCell *cell = (ProductsCell *)[aTableView dequeueReusableCellWithIdentifier:plainCellIdentifier];
if (!cell) {
cell = [[ProductsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:plainCellIdentifier];
}
Cart *ret = (Cart *)[self.fetchedResultsController objectAtIndexPath:indexPath];
// How to show the product names using the relationship?
cell.title = ret.products ??;
[cell setNeedsDisplay];
return cell;
}
推荐答案
显示特定产品购物车,您将创建获取结果控制器
,如下所示(未编译器检查,可能存在语法错误!):
To display product for one specific cart, you would create the fetched results controller as follows (not compiler checked, there may be syntax errors!) :
Cart *theCart = ... the cart that you want display products for ...
// Fetch request for "Product":
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Product"];
// Fetch only products for the cart:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inCart = %@", theCart];
[fetchRequest setPredicate] = predicate;
// Assuming that you want to sort by "navn":
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"navn" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_theManagedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
获取的对象现在是 Product
对象,因此,在 cellForRowAtIndexPath:
中你会这样做:
The fetched objects are now Product
objects, therefore in cellForRowAtIndexPath:
you would do for example:
...
Product *product = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.title = product.navn;
备注:获取的结果控制器使用缓存(就我而言)知道)仅用于部分信息,因此无需在此处指定 cacheName:
。
Remark: The fetched results controller uses a cache (as far as I know) only for section information, so there is no need to specify a cacheName:
here.
这篇关于获取关系并在UITableView中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!