views:

416

answers:

3

Hi,

I have set up the application with 2 entity, called Category1 and category2. There is a to-many relationship between category1 and category2. When a cell is pushed in tableview (category1), a new tableview will display all category2 cells related to category1. Here is what an example:

I have two category1 items in the first tableview, called Food and Snacks. Item "Food" have a subcategory (category 2) that contains 5 different kinds of food. Item "Snacks" have a subcateogry (category2) that contains 10 different snacks.

So when i push the food item (category1) I just want the food item to be loaded (5 of them). Right now, I can see all 5 of the food item in the tableView, plus the 10 items from the "snacks" category2.

I use this code in category2:

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Folder" inManagedObjectContext: tag.managedObjectContext];
 [request setEntity:entity];

 //NSMutableSet *filtered = [tag mutableSetValueForKey:@"folders"];

 // Order the events by creation date, most recent first.
 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
 [request setSortDescriptors:sortDescriptors];
 [sortDescriptor release];
 [sortDescriptors release];

 // Execute the fetch -- create a mutable copy of the result.
 NSError *error = nil;
 NSMutableArray *mutableFetchResults = [[tag.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
 if (mutableFetchResults == nil) 
 {
  // Handle the error.
  NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  exit(-1);  // Fail  
 }

 // Set self's events array to the mutable array, then clean up.
 [self setTagsArray:mutableFetchResults];
 [mutableFetchResults release];
 [request release];

need help !

thanks in advance!

A: 

Do it in views, so there are two views for the subcategories, one for food, one for snacks. Then on the first table view have it set so when you tap on food, it'll go to the food view, same with the snacks. If you can't get it working try with UIButtons. What I do is if all else fails, go to UIButtons :) If you need help getting it to switch views I made an example as part of my 25 apps in august project, you can find my example here: http://appeveryday.wordpress.com/2009/08/02/app-3/

Matt S.
Hi matt, thanks for the answer. However, I do think i need to clarify what i need to do. The fetch-code above will fetch all the items in all the sub-categories and put them in the array. What I want is just to fetch the items related to a specific category. How do I do this? thanks
ah. I see nowJust create an NSArray for each category, then call each array as needed in the project as the UITableView cells
Matt S.
+1  A: 

First a bit of advice: In your root table view you should be using a NSFetchedRequestController to manage the top level objects. Take a look at the Recipes application in Apple's sample code for a demonstration.

As for the primary issue you are seeing, what you should do is push the category1 object to the child view controller. The child view controller can then query the that category1 object for all of its children to display. No fetch is required on the child view controller because you already have the parent object. Again, the recipe example app from Apple will demonstrate this quite clearly for you.

Marcus S. Zarra
A: 

Thx Mitch for reply.

This is what I tried:

In my root view (category1), i did this to load the child view:

FolderViewController *folderViewController = [[FolderViewController alloc] initWithNibName: @"FolderViewController" bundle: nil];
folderViewController.tag = [tagsArray objectAtIndex: row]; [self.navigationController pushViewController: folderViewController animated: YES]; [folderViewController release];

Then in the childviewController, i use a sortdescriptor to get the objects:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

NSMutableArray *sortedIngredients = [[NSMutableArray alloc] initWithArray:[recipe.ingredients allObjects]];
[sortedIngredients sortUsingDescriptors:sortDescriptors];
self.ingredients = sortedIngredients;

[sortDescriptor release];
[sortDescriptors release];
[sortedIngredients release];

This works perfectly, but with one problem. I have set up my cells to display a checkmark when they are pushed, and I want the checkmark to be saved after the application have closed. So next time the app is loaded, the checkmark should be disblayed on the cells that have been clicked. I used this code to do that:

didSelectRowForIndexPath

if ([tag.folders containsObject: folder]) 
{
            cell.accessoryType = UITableViewCellAccessoryNone;
 [tag removeFoldersObject: folder];

}
else 
{
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 [tag addFoldersObject: folder];
}

The problem is with the: [tag removeFoldersObject: folder]. When the object is removed, the tableView will not display this row the next time. When I used the regular fetchrequest, this isn't a problem, but I get all childs object instead, as i explain in my first post.

Is there a solution to this challenge?

Thanks for any help