0

so i have two problems with the Checkmarks in UITableView with CoreData and NSUserDefault

  1. If i create a new object in the tableview, it is unchecked. When I check this object, its check. So thats all right... but when I create 10 objects and check the 5th object, delete it and refresh the tableView than the 6th object, which is now the 5th object, is checked, too. How can i fix this?

  2. When i delete an checked object, and create an object with the same name, refresh the UITableView, it gets checked just after creating it? wtf? :D

I guess NSUserDefault is the problem, but i don't know...

So here's my Code:

checkmark:

    - (BOOL) getCheckedForIndex:(int)index
    {
        if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }

    - (void) checkedCellAtIndex:(int)index
    {
        BOOL boolChecked = [self getCheckedForIndex:index];

        [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }



    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];

        //Use checkedCellAtIndex for check or uncheck cell
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        [self checkedCellAtIndex:indexPath.row];

        if([self getCheckedForIndex:indexPath.row]==YES)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

- (void) checkedCellAtIndex:(int)index
{
    BOOL boolChecked = [self getCheckedForIndex:index];

    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

TableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

    // Configure the cell...
    NSManagedObject *device = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {device = [searchResults objectAtIndex:indexPath.row];}
    else
        {device = [self.cart objectAtIndex:indexPath.row]; }

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"cartProductName"]]];
    cell.tintColor = [UIColor orangeColor];
    if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


    return cell;
}

i hope someone can help me. Thanks :)

user255368
  • 17
  • 6

1 Answers1

0

Answer to your Q1 :

    if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)

The above code checks for the index number in NSUserDefaults . So you save tags of cell objects / array objects instead of array index number .

EDIT: Since you are using Coredata you can add an BOOL attribute like : isChecked in the model classes. This way your database will also be having the checkmark info. Use the same to show checkmarks in the tableview. And btw : why are you using NSUserDefaults when you are using CoreData. I mean its like the old saying : Penny wise Pound Foolish

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
  • ah okay, seems legit. But what do i need to change? – user255368 Mar 21 '14 at 22:08
  • @user255368 : Pleae see my EDIT above – Abhishek Bedi Mar 21 '14 at 22:45
  • I've been thinking about your advice regarding saving the Checkmark with Core Data. What do i need to set it up? i found a solution here [link](http://stackoverflow.com/questions/2215465/how-can-a-checkmark-state-be-saved-in-core-data) but he uses the NSFetchedResultscontroller and i don't... which makes it quite hard for me to understand – user255368 Mar 22 '14 at 09:44