iosobjective-cuitableviewuiimageuitapgesturerecognizer

Change table view cell's UIImage on tap


I've got an image inside each cell of my table view controller and a tap gesture recognizer on it. That's working fine and logging appropriately.

What I need is for the image to change from its default state to then toggle between a "selected" state (green) and a "deselected" state (red). In other words, it's a checklist that's gray at first, then you tap the image, the image turns to a green checkmark, tap again, it turns to a red x.

Here's the catch: Had this working when it was only a matter of a conditional statement in the didSelectRowAtIndexPath method, but since I added the gesture recognizer and created the imageTapped method, I can't seem to translate it over. Therefore, otherwise helpful threads like this one don't work for me.

Here's the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PlacesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    //Create ImageView
    cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
    
    //Add Gesture Recognizer
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)];
    tapped.numberOfTapsRequired = 1;
    [cell.imageView addGestureRecognizer:tapped];
    cell.imageView.userInteractionEnabled = YES;
    [cell addSubview:cell.imageView];
    
    return cell;
    
    }

//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped
{
    
    UITableViewCell *cell;
    
    UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"];
    UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
    UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];
    
    if (cell.imageView.image == imageDefault) {
        cell.imageView.image = imageGreen;
        cell.selected = true;
        NSLog(@"Selected");
    } else {
        cell.imageView.image = imageRed;
        cell.selected = false;
        NSLog(@"Deselected");
    }
}

Solution

  • i have modified your code , check it

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"PlacesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    //Create ImageView
    cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
    
    //Add Gesture Recognizer
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
    tapped.numberOfTapsRequired = 1;
    [cell.imageView addGestureRecognizer:tapped];
    cell.imageView.userInteractionEnabled = YES;
    //[cell addSubview:cell.imageView]; 
    
    return cell;
    
    }
    
    //Method controlling what happens when cell's UIImage is tapped
    -(void)imageTapped:(UIGestureRecognizer*)gesture
    {
    
    UIImageView *selectedImageView=(UIImageView*)[gesture view];
    
    UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"];
    UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
    UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];
    
    if (selectedImageView.image == imageDefault) {
        selectedImageView.image = imageGreen;
        //cell.selected = true;
        NSLog(@"Selected");
    } else {
        selectedImageView.image = imageRed;
        //cell.selected = false;
        NSLog(@"Deselected");
    }
    }