Reputation: 324
I'm working on an simple core data app, that save a name of a list. I'm using a UIAlertView with a text field to enter the data. My problem is that when the user enters the data and hits save, it's not showing up in the TableView. But it was created beacese then when I re-run it there it is in the TableView.
So at this point I'm pretty lost I'm really new to core data so if you have any suggestions I'm all ears. Thanks!
Here is all the core data code in my TableView.m:
@implementation ViewController
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"List"];
self.lists = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
and
-(IBAction)add:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add List" message:@"Create a New Wish List" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert setTag:2];
[alert show];
alert.delegate = self;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 0 && alertView.tag == 2) {
UITextField *tf = [alertView textFieldAtIndex:0];
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
NSManagedObject *newList = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:context];
[newList setValue:tf.text forKey:@"name"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.lists objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
// Remove device from table view
[self.lists removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
@end
If you need anything else let me know! Thanks!
Upvotes: 0
Views: 182
Reputation: 70946
It looks like you're storing your table data in an array called self.lists
. Meaning that, for your table to show the data, you need to update the contents of that array. It doesn't look like you're doing that after creating a new entry, because your alert view callback doesn't update the contents of self.lists
.
You don't appear to be sorting self.lists
(at least, there's no sort descriptor on your fetch request), so you could just add the new entry to the array directly. It's a mutable array, so just add the new object. Then tell the table view that you added a new row (similar to the way you're currently removing a row when deleting an entry).
If you decide to start sorting the table at some point, you'll be better off updating self.lists
from your persistent store. Basically, take your code from viewDidLoad
that fetches data from Core Data and repeat those steps when adding (or deleting) entries.
You may want to look at using NSFetchedResultsController
, which is designed to simplify combining Core Data with table views.
Upvotes: 1