我有两个tableViews,主要是添加父对象,当你点击tableView中的父对象时,它会将你带到子tableView,我成功完成了第一部分,但是当它取出时“RIGHT”子对象我感到困惑,我这样做的方法是我获取所有子对象,使用枚举我选择正确的对象然后放入NSSet,但这不起作用,这里是子对象表视图.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.
}
- (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;
}
- (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;
}
- (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];
}
- (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中吗?
答案 0 :(得分:1)
您似乎正在使用NSFetchedResultsController来获取所有MinorGoal实例,但您只想显示子MinorGoals。如果要使用NSFetchedResultsController,则需要向fetch添加谓词,以便它仅返回作为所选目标的子项的MinorGoals。假设您在MinorGoal和Goal之间创建了一个名为“parentGoal”的关系:
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"self.parentGoal = %@", self.selectedGoal];
或者,不要使用NSFetchedResultsController而是使用该关系。您需要将NSSet转换为数组并将其排序为上一个答案。
答案 1 :(得分:0)
将您的父对象存储在子视图控制器中的某个位置,通常应该像
.h文件:
@property (nonactomic, strong) ParentObject *parentObject;
.m文件
- (id)initWithObject:(ParentObject *)parent {
…
_parentObject = parent;
…
}
然后获取:
(你应该有@property (nonatomic, strong) NSFecthedResultsController *fetchedResultController
)
- (NSFetchedResultsController*)fetchedResultController {
if (_fetchedResultController) {
return _fetchedResultController;
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:NSStringFromClass([ChildObject class])];
[request setEntity:entity];
NSSortDescriptor *sortDescription = [NSSortDescriptor sortDescriptorWithKey:@"sortOrder" ascending:YES];
[request setSortDescriptors:@[sortDescription]];
request.predicate = [NSPredicate predicateWithFormat:@"(parent == %@)", self.parentObject];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[NSManagedObjectContext mainThreadContext] sectionNameKeyPath:nil cacheName:nil];
controller.delegate = self;
_fetchedResultController = controller;
return _fetchedResultController;
}
然后实现方法
- (void)performFetch {
NSError *error = nil;
[self.fetchedResultController performFetch:&error];
if (![self.fetchedResultController performFetch:&error]) {
[self showAlertWithError:error];
}
else {
[self.tableView reloadData];
}
}
基本上你会收到你需要按谓词排序和过滤的所有子对象
答案 2 :(得分:0)
似乎我正在以艰难的方式去做,只需创建将保存子对象的新数组。
NSMutableArray *minors = self.selectedGoal.minorGoals.allObjects.
当你获得更改或添加时,执行两次,一次执行数组,另一次使用“self.selectedGoal.managedObjectContext”。