Reputation: 632
I have a tableview and i made a custom cell with two textfields...i want the text in the textfields to be saved in core data, so i created the core data class for that purpose and i know how to implement the core data methods.
I´m having trouble understanding how can i display the cell in the tableview, so that the user enters the text and then he get´s saved in core data. From my understanding these are the steps:
1 - the tableview loads and displays the custom cell by default with the textfields.
2 - the user enters the text, hits enter and he get´s saved in core data.
My problem is how can i perform number 1, i cannot load data in view did load because there is no data YET!, i have to display the cell first.
Can anyone help me with this issue, let me understand what am i missing here? this is my code so far:
- (void)viewDidLoad
{
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
self.managedObjectContext=[appDelegate managedObjectContext];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[self fetchedResultsController];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"machines";
cellMeios *cellM = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cellM == nil) {
cellM = [[cellMeios alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cellM;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
// Create and configure a fetch request.
NSFetchRequest *fetchRequestMo = [[NSFetchRequest alloc] init];
NSEntityDescription *entityMo = [NSEntityDescription entityForName:@"Mo" inManagedObjectContext:self.managedObjectContext];
[fetchRequestMo setEntity:entityMo];
// Create the sort descriptors array.
NSSortDescriptor *cellTitle = [[NSSortDescriptor alloc] initWithKey:@"reference" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:cellTitle, nil];
[fetchRequestMo setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequestMo managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"reference" cacheName:nil];
_fetchedResultsController.delegate = self;
self.fetchedResultsController = _fetchedResultsController;
return _fetchedResultsController;
}
There are the other methods but i think they are not relevant for my problem, the thing is...i need to display something to the user so that he can fill up!
I already have another tableview working with core data but in that case, the table is supose to be empty at first, then there´s a plus button on the top of the tableview and THEN he choses something and it get´s saved in core data displaying a cell with relevant information.
Thanks in advance Regards.
Upvotes: 0
Views: 269
Reputation: 1727
If you intend the table view to be editable, you should have a method like insertNewObject that would be triggered when the user hits the '+' (plus) button in the navigation bar. The implementation of this method creates a new managed object, inserts it in the context, and reloads the table. You could also trigger this method when the fetchedResultsController comes back empty.
Here's a version from one of my view controllers:
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.managedObject managedObjectContext];
NSRelationshipDescription *relation = [[self.managedObject entity] relationshipsByName][self.relationship];
NSEntityDescription *destinationEntity = [relation destinationEntity];
NSManagedObject *newManagedObject = (NSManagedObject*)[NSEntityDescription insertNewObjectForEntityForName:[destinationEntity name] inManagedObjectContext:context];
if ([relation isOrdered]) {
[self.items insertObject:newManagedObject atIndex:[self.items count]];
}
else {
NSMutableSet *set = [self.managedObject mutableSetValueForKey:self.relationship];
[set addObject:newManagedObject];
self.items = [NSMutableOrderedSet orderedSetWithSet:set];
}
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Validation Error", @"Validation Error") message:[error localizedDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles: nil, nil] show];
}
[self.tableView reloadData];
}
Upvotes: 2