Reputation: 823
Im trying to create a listing app that is core data backed.The first page lets you create as many list as you want and name them. By tapping on a name in the first view, you will be taken to that actual list where you can create the items in that list. However if I create 3 lists they each have the same data. Do I need a different store with each first view cell? Also if I do how do I make it create a new store with every new cell that is added? Thanks for any help.!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//This is the second view that holds the list items
RootViewController *secondViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
secondViewController.managedObjectContext = [self managedObjectContext];
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
}
Rootview.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell to show the book's title
Book *book = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = book.title;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
return nil;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
}
Upvotes: 0
Views: 127
Reputation: 64428
You design a data model based not on how the data will be displayed but instead on how the data logically relates to itself. Each entity should model a real world object, event or condition with no regard to how that data will end up being displayed.
In this case, you are modeling books and a user supplied organization of those books into list. You need a book entity to model the books and a list entity to model the users organization. (It doesn't have to be a "list" it could be "collection", "shelf" etc depending on what you're trying to model.)
Presumably, each book can be in different list and each list may contain many books.
Book {
author:string;
copyright:sting/date;
item:(?);
title:sting;
list<<-(optional,nullify)-->>list.books
}
List {
name:string;
books<<--(optional,nullify)-->>book.list;
}
In your UI, you would have the fetched results controller (FRC) fetch all list entities and then display the name of each in a table view. When a table row is selected, you push the next view and hand it the NSSet returned by list.books of the list object associated with that row.
In the next table view, each row is populated from one or more attributes of each book entity in the passed set. When row is selected, you push a book detail view and pass it the book entity associated with the selected row.
Notice that from the perspective of the data model, the UI is irrelevant. You could be displaying the data in web view or even a text based command line. All the data model cares about are the contents of the entities and the logical relationships between them.
Design your data model to handle the logical relationships in your data. Once you do that, the UI becomes very simple to implement regardless of the format because the UI is not responsible for maintaining the integrity of the data model and the data model does not have to worry about the state of the UI.
Upvotes: 3
Reputation: 3158
No you definitely do not need a separate store for each cell added.
You need a Datamodel which fits your requirements. List ----- ListEntry.
The first page shows all "List" entities, after selecting a list, all "ListEntries" with a relation to "List" are displayed.
Upvotes: 2