Will Hua
Will Hua

Reputation: 1748

TapGesture in UITableViewCell causing other cells to have altered state

I'm making my first iOS app and I have a UITableView with a list of UITableViewCells. Each UITableViewCells has two labels:

@property (weak, nonatomic) IBOutlet UILabel *upvote;
@property (weak, nonatomic) IBOutlet UILabel *downvote;

These labels each have a gesture recognizer. When the label is tapped, both label's have their alpha set to 0 through an animation.

-(void) handleSingleTapGesture: (UITapGestureRecognizer *)gestureRecognizer
{
    UILabel *vote = (UILabel*)[gestureRecognizer view];
    AppCell *appCell = (AppCell*)vote.superview.superview;

    [UIView animateWithDuration: 1.0 animations:^(void) 
    {
        appCell.upvote.alpha = 0;
        appCell.downvote.alpha = 0;
    }
}

Here is the cellForRawAtIndexPath method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{
    AppCell *cell = (AppCell *)[tableView dequeueReusableCellWithIdentifier:@"AppCell"];
    App *app (self.apps)[indexPath.row];

    /** setting the text/fonts/alpha for other labels I get from a HTTP POST request. Did not include the code since not relevant */

    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];

    cell.upvote.font = [UIFont fontWithName:@"Roboto-Light" size:14];
    [cell.upvote setUserInteractionEnabled:YES];
    [cell.upvote addGestureRecognizer:singleTapGestureRecognizer];

    *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];

    cell.downvote.font = [UIFont fontWithName:@"Roboto-Light" size:14];
    [cell.downvote setUserInteractionEnabled:YES];
    [cell.downvote addGestureRecognizer:singleTapGestureRecognizer];

}

It works as it should. When I tap the text, they both disappear. However, it seems that as I scroll down some of the future cells don't have the UPVOTE/DOWNVOTE text.

I'm pretty confident that it has something to do with the reuse of cells but I do not know how to fix it.

I have looked up potential solutions and I can't seem to find much. The only thing I could find was that it was more efficient adding gesture recognizers to the UITableView instead of the UITableViewCell. I can't do that because I don't want an action to occur when the cell is tapped, only when one of the two labels is tapped.

If more information is required/there is a solution that I missed on StackOverflow please let me know.

Upvotes: 0

Views: 69

Answers (1)

bobnoble
bobnoble

Reputation: 5824

As you guessed, it likely has to do with the reuse of cells. The comment in the cellForRowAtIndexPath methods says it handles the alpha but there is no setting of alpha in the method, so...

When a cell is reused, the upvote and downvote label alpha is whatever it was last set to, either implicitly (defaults to 1 when the UILabel is created) or explicitly (set to 0 by the gesture handler).

You'll need to keep track of the votes for each cell, and set the alpha appropriately in the cellForRowAtIndexPath method.

Upvotes: 1

Related Questions