0

my viewController contains a tableView where the user can add cells via coreData. I defined the cells:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Travel";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];

        UILabel *countryLabel = (UILabel *)[cell viewWithTag:101];
        kategorieLabel.text = [travel valueForKey:@"country"];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
        [cell addSubview:button];

            if ([[travel valueForKey:@"tick"] isEqualToString:@"yes"]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
        return cell; 
    }

- (void)buttonTapped:(UIButton *)sender {
    UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
    [owningCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    NSIndexPath *indexPath = [_tableView indexPathForCell:owningCell];
    NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
    [travel setValue:@"yes" forKey:@"tick"];
    }

When the user taps a button on the cell, the checkmark should appear. But when I reload or restart the app not all checkmarks are there. Maybe there is a better method to do this.

user2650439
  • 207
  • 1
  • 3
  • 8
  • Do you store `einkauf` (whatever that is) state/data? – Kreiri Aug 09 '13 at 15:42
  • sorry...I've renamed it...it's `travel` – user2650439 Aug 09 '13 at 15:50
  • You need to save the changes with core data. – Kevin Aug 09 '13 at 15:55
  • @Kevin I thought I did this with my las line of code – user2650439 Aug 09 '13 at 16:01
  • 1
    @user2650439 You are setting the data, but not saving it. I believe you need to call [`-[NSManagedObjectContext save:]`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/save:) on your context. – Kevin Aug 09 '13 at 16:17
  • Why is there no "if (cell == nil) { cell alloc }" in your cellForRowAtIndexPath? – bachonk Aug 09 '13 at 16:38
  • @bachonk because he's using [dequeueReusableCellWithIdentifier:forIndexPath:](http://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006943-CH3-SW92) – geraldWilliam Aug 09 '13 at 16:58
  • @Kevin But when I only reload this viewController (embedded in a navigationController) it works well – user2650439 Aug 10 '13 at 13:32
  • Yes, changes stay in memory while the program is running, unless you actively discard them, but they don't get written to disk until you save. So if you don't save, the next time you run the program it will only have the old data. – Kevin Aug 10 '13 at 14:45
  • Ok...I understand...now everything works perfectly :) – user2650439 Aug 10 '13 at 18:31

0 Answers0