views:

34

answers:

0

I started learning core data and I wanted to do a simple category->object->detail application. I started with the location tutorial. After I've done it, I moved all the auto-generated code from my appDelegate to a new DataProvider class and i put a tabbar at the beginning of the app. So I have a TableView tabbar item (with a navigation controller) and a manageDB tabbar item where I put some button to add/remove categories or Objects.

I haven't linked the buttons from the manageDB view with anything yet. I'm still trying to make it work. I kept the addEvent plus button from the location tutorial in my tableView so I'm adding categories from there. Categories are added, deleted and saved as they're supposed to. I made the same thing for the object table view but the objects don't get saved when i exit the app.

//this is where I push the object-table view when someone taps a category

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    Category *category = (Category *)[categoriesArray objectAtIndex:indexPath.row];

    cell.textLabel.text = @"category";

    return cell;
} 


//and this is from the objectviewcontroller

- (void)viewDidLoad {
    [super viewDidLoad];  

    self.title = @"Objects";
    self.objectsArray = [NSMutableArray arrayWithArray:[[Storage instance] fetchObjectsForCategory:self.category]];


    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addObject)];
    addButton.enabled = YES;
    self.navigationItem.rightBarButtonItem = addButton;   

    NSLog(@"%@", self.managedObjectContext);
}

- (void)addObject {
    Object *object = (Object *)[NSEntityDescription insertNewObjectForEntityForName:@"Object" inManagedObjectContext:self.managedObjectContext];

    [objectsArray insertObject:object atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    [[Storage instance] saveContext];    
}