获取子对象属于父对象&把它们放在tableView(核心数据,关系) [英] Fetching child objects belongs to parent object & put them in tableView (core-data, relationships)
问题描述
我有两个tableViews,主要的一个添加父对象,当你点击一个父对象在tableView,它带你到子tableView,我做的第一部分成功,但当它涉及到获取RIGHT子对象我很困惑,我的方式我做的是,我获取所有子对象,使用枚举我选择正确的,然后放入一个NSSet,但是不工作,这里是子对象表视图.m:
I have two tableViews, the main one adds the parent object, and when you click on a parent object in the tableView, it takes you to the child tableView, I did the first part successfully, but when it comes to fetching the "RIGHT" child objects I get confused, I way I do it is that I fetch ALL child object, the using enumeration I pick the right ones and put then into a NSSet, but that doesn't work, here's the child object table view.m:
#import "MinorGoalsTableViewController.h"
@interface MinorGoalsTableViewController ()
@end
@implementation MinorGoalsTableViewController
@synthesize selectedGoal = _selectedGoal;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize minorGoalsSet;
// init with goal (for GTVC)
- (id) initWithGoal:(Goal *)goal {
if (self = [super init]) {
_selectedGoal = goal;
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error = nil;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSLog(@"Minor Goals in %@ are: %lu", self.selectedGoal.title , (unsigned long)[self.selectedGoal.minorGoal count]);
// self.minorGoalsSet = nil;
// initializing minorGoalsSet
self.minorGoalsSet = [[NSMutableSet alloc] init];
// performing fetch
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Error fetching all minor goals: %@", error);
abort();
}
// creating NSSet that will carry all selectedGoal minor goals
NSSet *minorGoals = self.selectedGoal.minorGoal;
// creating a loop to add minor goals in minorGoalsSet
// add existing minor goals in selected goal to minorGoalsSet
for (MinorGoal *minor in minorGoals) {
[minorGoalsSet addObject:minor];
}
NSLog(@"minor goals in set: %lu", (unsigned long) [minorGoalsSet count]);
NSLog(@"minor goals in set2: %lu", (unsigned long) [minorGoals count]);
// setting nav title to selected goal title
self.navigationItem.title = _selectedGoal.title;
// adding "add" button to nav
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewMinorGoal)];
}
- (void) viewWillDisappear:(BOOL)animated {
self.selectedGoal = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
pragma mark - 表视图数据源
pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.selectedGoal.minorGoal count];
// id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
// return [secInfo 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];
}
cell.accessoryType = UITableViewCellAccessoryNone;
// Configure the cell...
MinorGoal *minor = [self.fetchedResultsController objectAtIndexPath:indexPath];
// well see about that later
if ([minorGoalsSet containsObject:minor]) {
cell.textLabel.text = minor.title;
}
// setting cell's title to minor goal's title
// cell.textLabel.text = minor.title;
return cell;
}
pragma mark - 提取结果控制器方法
pragma mark - fetched results controller method
- (NSFetchedResultsController*) fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
// creating fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MinorGoal"
inManagedObjectContext:self.selectedGoal.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// setting _fetchedResultsController
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.selectedGoal.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
// performing fetch
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Error fetching minors: %@", error);
}
// returning
return _fetchedResultsController;
}
pragma mark - 表视图委托
pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void) addNewMinorGoal {
// code to show UIAlertView that will add new minor goal
// creating UIAlertView
UIAlertView *addMinorGoalAlert = [[UIAlertView alloc] initWithTitle:@"Add Minor Goal" message:@"Minor Goal Title" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
// Adding plain textfield for minor goal title
addMinorGoalAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
// showing UIAlertView
[addMinorGoalAlert show];
}
pragma mark - UIAlertView用于添加新的小授权
pragma mark - UIAlertView for Add New Minor Delegation
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"Adding Minor Goal Canceled!");
}
else
{
// creating string will carry textField value
NSString *minorTitle = [[alertView textFieldAtIndex:0]text];
// creating new minor goal
MinorGoal *newMinorGoal = [NSEntityDescription
insertNewObjectForEntityForName:@"MinorGoal"
inManagedObjectContext:self.selectedGoal.managedObjectContext];
// setting title of new minor goal
newMinorGoal.title = minorTitle;
[self.selectedGoal addMinorGoalObject:newMinorGoal];
// saving
NSError *error = nil;
if (![self.selectedGoal.managedObjectContext save:&error]) {
NSLog(@"Error saving new minor goal: %@", error);
}
NSLog(@"we save %@ to %@ and theres %lu in it", newMinorGoal.title, self.selectedGoal.title, (unsigned long) [self.selectedGoal.minorGoal count]);
// fetching
[self.fetchedResultsController performFetch:&error];
// reloading tableView data
[self.tableView reloadData];
}
}
@end
推荐答案
看起来你正在使用一个NSSet一个NSFetchedResultsController来获取所有MinorGoal实例,但是你只想显示子MinorGoals。如果要使用NSFetchedResultsController,则需要向访存中添加一个谓词,以便它只返回作为所选目标的子代的MinorGoals。假设你已经在MinorGoal和Goal之间创建了一个名为parentGoal的关系:
It appears that you are using a NSFetchedResultsController to get ALL MinorGoal instances, but you just want to show the child MinorGoals. If you want to use an NSFetchedResultsController then you need to add a predicate to the fetch so that it only returns the MinorGoals that are children of the selected Goal. Assuming that you have created a relationship between MinorGoal and Goal called "parentGoal":
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"self.parentGoal = %@", self.selectedGoal];
或者,不要使用NSFetchedResultsController而是使用关系。您将需要将NSSet转换为数组,并将其排序为上一个答案。
Alternatively, don't us the NSFetchedResultsController and instead use the relationship. You will need to convert the NSSet to an array and sort it as the previous answer.
这篇关于获取子对象属于父对象&把它们放在tableView(核心数据,关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!