Pavel Kaljunen
Pavel Kaljunen

Reputation: 1291

Detect on which button need to change text in cell

I have like button in tableviewcell

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if ( !cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

        DBImageView *imageView = [[DBImageView alloc] initWithFrame:(CGRect){ 0, 0, 320, 320 }];
        [imageView setPlaceHolder:[UIImage imageNamed:@"Placeholder"]];
        [imageView setTag:101];
        [cell.contentView addSubview:imageView];
    }


    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
    [(DBImageView *)[cell viewWithTag:101] setImageWithPath:[tmpDict objectForKey:@"thumb_link"]];


    likeButton = [UIButton buttonWithType:UIButtonTypeCustom] ;
    [likeButton setFrame:CGRectMake(cell.frame.size.width-60,280,81,33)];
    likeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    likeButton.contentEdgeInsets = UIEdgeInsetsMake(3, 2, 0, 0);
    [likeButton.titleLabel setFont:[UIFont fontWithName:@"Trebuchet MS" size:16]];
    [likeButton setBackgroundImage:[UIImage imageNamed:@"heart.png"] forState:UIControlStateNormal];
    [likeButton setBackgroundImage:[UIImage imageNamed:@"heart_pressed.png"] forState:UIControlStateSelected];
    [cell addSubview:likeButton];


    // like button
    likeButton.selected = ![likeButton isSelected];
    NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]];
    if (defaultKey2 == 0)
    {
        //[likeButton setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
        [likeButton setSelected:NO];
    }
    else if (defaultKey2 == 1)
    {
        //[likeButton setTitleColor:[UIColor colorWithRed:229/255.0 green:85/255.0 blue:140/255.0 alpha:1.0] forState:UIControlStateNormal];
        [likeButton setSelected:YES];
    }


    [likeButton setTitle:[tmpDict objectForKey:@"likes"] forState:UIControlStateNormal];
    [likeButton addTarget:self action:@selector(like:) forControlEvents: UIControlEventTouchUpInside];
    [likeButton setTag:indexPath.row];

    return cell;

}

This is my action:

-(void)like:(id) sender{
    UIButton *likebtn = (UIButton *)sender;
    NSDictionary *tmpDict = myObject[likebtn.tag];
    NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]];
    if (defaultKey2 == 0)
    {
        //Increase value
        int varTemp = [[tmpDict objectForKey:@"likes"] intValue]+1;
        NSString *strValue = [NSString stringWithFormat:@"%d", varTemp];
        [likeButton setTitle:strValue forState:UIControlStateNormal];
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setInteger:1 forKey:[tmpDict objectForKey:@"id"]];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    if (defaultKey2 == 1)
    {
        //Decrease value
        int varTemp = [[tmpDict objectForKey:@"likes"] intValue]-1;
        NSString *strValue = [NSString stringWithFormat:@"%d", varTemp];
        [likeButton setTitle:strValue forState:UIControlStateNormal];
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setInteger:0 forKey:[tmpDict objectForKey:@"id"]];
        [[NSUserDefaults standardUserDefaults]synchronize];    
    }
    NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:likebtn.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

When I try to set title for my button in cell [likeButton setTitle:strValue forState:UIControlStateNormal]; the value goes to wrong cell.

How can I detect for which button in cell value need to be set? How can I use my tag?

Upvotes: 0

Views: 140

Answers (2)

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

You can't set the button title that way in your like: method, i.e.:

[likeButton setTitle:strValue forState:UIControlStateNormal];

(1) Because you're reloading the table right afterwards thus manually triggering a call to cellForRowAtIndexPath: and (2) even if you didn't reload the table data, even scrolling would trigger a call to cellForRowAtIndexPath and thus your cells would be populated based on cellForRowAtIndexPath:'s information.

As it stands now, your cellForRowAtIndexPath: method is using the following line to set the title:

[likeButton setTitle:[tmpDict objectForKey:@"likes"] forState:UIControlStateNormal];

That means that in order for the like: action to trigger the title change, you have to change the [tmpDict objectForKey:@"likes"] value in like:. Since you're using a temporary dictionary and never add the dictionary back into the myObject array, you haven't changed the value that ultimately decides your button's title in cellForRowAtIndexPath:. So you have to update myObject accordingly. Here's my code suggestion:

-(void)like:(id)sender {

    UIButton *likebtn = (UIButton *)sender;
    NSMutableDictionary *tmpDict = myObject[likebtn.tag];
    NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]];

    if (defaultKey2 == 0)
    {
        //Increase value
        int varTemp = [[tmpDict objectForKey:@"likes"] intValue]+1;

        // Then update myObject
        [tmpDict setObject:[NSString stringWithFormat:@"%d", varTemp] forKey:@"likes"];
        [myObject replaceObjectAtIndex:likebtn.tag withObject:tmpDict];

        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setInteger:1 forKey:[tmpDict objectForKey:@"id"]];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    if (defaultKey2 == 1)
    {
        //Decrease value
        int varTemp = [[tmpDict objectForKey:@"likes"] intValue]-1;

        // Then update myObject
        [tmpDict setObject:[NSString stringWithFormat:@"%d", varTemp] forKey:@"likes"];
        [myObject replaceObjectAtIndex:likebtn.tag withObject:tmpDict];


        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setInteger:0 forKey:[tmpDict objectForKey:@"id"]];
        [[NSUserDefaults standardUserDefaults]synchronize];    
    }

    NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:likebtn.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationNone];

}

Edit: Also there's a big problem now that I've seen your cellForRowAtIndexPath: method. You've declared the button as a class variable? (1) Don't do that. Keep it local, ex:

UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom] ;

(2) You're reusing your cells, but adding a new button each time. So you're adding one button on top of another on top of another with each cellForRowAtIndexPath: call. This could cause problems for you going forward.

Upvotes: 1

Bin Chen
Bin Chen

Reputation: 140

Your cell may reuse the buttons and tags can be disordered. Try to refresh your bouttons' tags in tableView:willDisplayCell:forRowAtIndexPath: method.

Upvotes: 0

Related Questions