views:

26

answers:

1

Hey, i have question related to CoreData. My iphone project has 2 Entities, Organisation and Brand with a 1 to many "BrandsToOrg" relationship and inverse.

So my project has a Mapview, where you can see all the Organisations and a little subview when you click on those Organisations.At the subview there is a "show Brands" Button, which should init a new TableView who only shows the brands belong to the seleceted organisation. Any ideas how i can code that?

thx

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(TitleMedium == %@)",@"Rock Antenne"];????
NSFetchRequest *request = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:self.entityName inManagedObjectContext:managedObjectContext];
  [request setEntity:entity]; 


  if(predicate != nil)
  {
   [request setPredicate:predicate];
  }

  // If a sort key was passed, use it for sorting.
  NSString *sortKey=@"TitleMedium";
  BOOL sortAscending=YES;
  if(sortKey != nil)
  {
   NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
   NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
   [request setSortDescriptors:sortDescriptors];
   [sortDescriptors release];
   [sortDescriptor release];
  }

  NSError *error;

  NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

  [request release];
  [self setEntityArray:mutableFetchResults];
A: 

Here's an example of how you would do this with a top-level UITableView of orgs and a detailed table view of brands. At the top level, you do a fetch request for the orgs and display them. When a particular one is selected, you create the detail view of brands. The BrandsViewController object has a "org" property that tells is what to display; it can then refer to "org.brands" (which is the Core Data one-to-many relationship) to locate all the brands for that display. Note that you don't need to do an explicit Core Data fetch within the BrandsViewController to get the brands.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

BrandsViewController *targetViewController = [[BrandsViewController alloc]
    initWithNibName:@"BrandsView" bundle:nil];
    // pass managedObjectContext along to new view controller
targetViewController.managedObjectContext = self.managedObjectContext;

// Pass the selected org to the new view controller
targetViewController.org = (Organisation *)[fetchedResultsController objectAtIndexPath:indexPath];

[[self navigationController] pushViewController:targetViewController animated:YES];
[targetViewController release];
}
David Gelhar
hey, i have this working with a Org Tableview and when you select a Row you get the Brand Detail Tableview. That's all working so far and thats one way to display the brands.The other way should be the Mapview which shows the location of the Organisations and then you should press the button to see the brands. And there is my problem that i only get all brands an not the specific one of the organisations.
Chris Summer
Sounds like your MapView first needs to figure out which org is at the selected location, then pass the selected org on to your detail tableview somehow.
David Gelhar