0

I have a test for whether a cell was clicked by the user. If it was clicked, the image changes. I want that image to remain checked, even if the user clicks it again or the app reloads.

It's really close to working. Right now, the image changes from the grey version to the green version when the cell was selected. The problem is the grey image shows back up when the user clicks the cell again. What do I need to do to fix this?

 //begin checking for selected row and add checkmark
- (NSString *)getKeyForIndex:(int)index
{
    return [NSString stringWithFormat:@"KEY%d",index];
}

- (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];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableList count];
}


// Customize the content and the look of table view cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UIImage *greyck =[UIImage imageNamed:@"greycheck-sd.png"];
    UIImage *greenck =[UIImage imageNamed:@"greencheck-sd.png"];

    //step 1 check to see if we can reuse a cell that has just rolled off the screen
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath: indexPath];

    //step 2: if there are no cells to be reused, create a new cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    //step 3: set the text in the cell using items from the array
    cell.textLabel.text  = [tableList objectAtIndex:indexPath.row];

    //set custom font
    cell.textLabel.font = [UIFont fontWithName:@"Chalkboard SE" size:18.0f];


    //check for previously viewed then set the image
    if([self getCheckedForIndex:indexPath.row]==YES)
    {
        //sets green checkmark after user clicks
        cell.imageView.image = greenck;
    }
    else
    {
        cell.imageView.image = greyck;
    }

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *greyck =[UIImage imageNamed:@"greycheck-sd.png"];
    UIImage *greenck =[UIImage imageNamed:@"greencheck-sd.png"];

    NSString *str= [tableList objectAtIndex:indexPath.row];
    if ([str isEqual:@"introduction"])
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *moviePath = [bundle pathForResource:@"Step1-Intro" ofType:@"mp4"];
        NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

        MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        theMovie.scalingMode = MPMovieScalingModeAspectFill;
        [theMovie play];
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
        [self presentMoviePlayerViewControllerAnimated:moviePlayer];

    }

    else if ([str isEqual:@"skating"])
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *moviePath = [bundle pathForResource:@"Step2-Skating" ofType:@"mp4"];
        NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

        MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        theMovie.scalingMode = MPMovieScalingModeAspectFill;
        [theMovie play];
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
        [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    }




    [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.imageView.image = greenck;
    }
    else
    {
        cell.imageView.image = greyck;
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
CharleyB
  • 41
  • 1
  • 6
  • Setting green or grey checkmark depends on the method getCheckedForIndex:. So, please post your code for this method. – Reinhard Männer Dec 08 '13 at 18:55
  • Thank you for responding, Herr Männer. The applicable code is posted above. Would you like to see ALL of the code? I am not using an accessory indicator. I am using an image. cell.imageView.image = greenck; – CharleyB Dec 08 '13 at 18:59
  • Actually, you did not post the code of the method "getCheckedForIndex:", but your decision (green/grey) depends on it. Without it, it is hard to tell what is wrong. – Reinhard Männer Dec 08 '13 at 19:55
  • I think you just solved my issue. I was trying to code with a toddler in the room and forgot that I deleted it. Now, I forgot what I did. Suggestion? (I am very new to Objective-C. Be gentle.) – CharleyB Dec 09 '13 at 07:34
  • If I understand your right, you deleted code that you still need. This is no problem, if you use source control in XCode (extremely highly recommended) and if you ever committed your app: Just open your version editor, and it will show you the older version(s). If you by chance did not use source control, you could still try to recover the old version from a backup like time machine. Good luck! – Reinhard Männer Dec 09 '13 at 10:08
  • Got it. Thanks. What did I miss? – CharleyB Dec 09 '13 at 18:51
  • Did you solve your problem ? I there anything left? – Reinhard Männer Dec 09 '13 at 19:40
  • No. It almost works. If I click on the cell, the green check image shows up. If I click it again, it goes away. I want it to remain there after the first click. The intent is for the user to be able to keep track of what movies they've watched. – CharleyB Dec 09 '13 at 19:43

1 Answers1

0

You do set the displayed image to green at 2 places with

   if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.imageView.image = greenck;
    }

But getCheckedForIndex: returns YES

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

So far, so good. But it seems to me that you never update your user defaults. If so, you always would return the same value, and nothing will change.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • Sir, I don't understand. Should I replace one of the tests with the NSUserDefailts code above? I tested with only one if/else and it failed to function at all. – CharleyB Dec 11 '13 at 09:32
  • What I wanted to say is that your method "getCheckedForIndex:" returns YES or NO, depending on your UserDefaults settings. But it seems to me that you never change these settings. If so, your image will always be set to the same color. – Reinhard Männer Dec 11 '13 at 11:14
  • It does change color on selection. It changes back when clicked again. How do I set it so it remains green? I wish I could post an image, or video. – CharleyB Dec 11 '13 at 16:08
  • If I understand correctly, I need a new function that sets the default user settings after the user views a video that sets the image to green. How would I do that? – CharleyB Dec 12 '13 at 09:34
  • I am not sure if I am right anyway, but my impression is that you should make your decision which color to take not based on the user defaults (which should be rather stable), but on an instance variable that you set appropriately. – Reinhard Männer Dec 13 '13 at 11:29
  • I followed this to get to where I am. What should I change so the the user cannot change it back to grey? http://stackoverflow.com/questions/12120696/how-to-save-tableview-cells-checkmark-after-reload-view-use-nsuserdefaults – CharleyB Dec 13 '13 at 18:39