0

Is it possible to update a value in the entity from the configure cell function? I want to change the value of an entries attribute if a number is greater than the other. This is what i have so far but it crashes the app with an error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<Outgoing 0x7fbcf3141890> setValue:forUndefinedKey:]: the entity Outgoing 
is not key value coding-compliant for the key "goingover".'

MY configurecell method:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    Outgoing *outgoing = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = outgoing.costDescription;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f",[outgoing.amount floatValue]];

    if (self.incomingTotal > self.outgoingTotal) {

        [outgoing setValue:@"no" forKey:@"goingover"];

    }


    if ([outgoing.goingOver isEqualToString:@"yes"]) {
        cell.backgroundColor = [UIColor redColor];
    }else{

        cell.backgroundColor = [UIColor lightGrayColor];

    }

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
John
  • 63
  • 1
  • 6
  • Is your attribute named "goingOver" rather than "goingover" (the names are case sensitive). – pbasdf May 03 '15 at 23:03

1 Answers1

0

I presume that in your Outgoing class, which should be a subclass of NSManagedObject, you have not properly defined the attribute goingover with this exact name and type NSString.

Besides, it is a bad idea to update a core data entity in the configureCell method. You should separate your views (cell) and your data (managed object). This method is for configuring the cell, not the data. Instead, before loading any cell, make sure the data is correct (according to the rule you have defined in your if statement.

Finally, it not advisable to store a boolean (true or false) in a string. You should modify your data model to use a boolean. Also, such as simple condition probably does not need to be persisted at all. Just use the if clause to color your cell.

Mundi
  • 79,884
  • 17
  • 117
  • 140