Table view return wrong values when scrolling

2019-08-20 05:29发布

The problem is that I have implemented a table view with custom cell from storyboard. Each cell has a button as a selection button which mean whenever I press on a button , it image change. For example, if I press on the button with cell index=0, it image change correctly however when scrolling the table I found other buttons also changed their images! The problem is from the table index path and I spent a lot of time trying to fix it with no result. Any Solution?

Thank you

    static NSString *simpleTableIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
        NSLog(@"index path inside cell = nil %i",(int)indexPath);
    }

    buttonObj=(UIButton *)[cell viewWithTag:40];
   [buttonObj addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchDown];

return cell;

-(void) select:(id)sender{
    UIButton *button=(UIButton *)sender;

    UITableViewCell *clickedCell = (UITableViewCell*)[[sender superview] superview];
    NSIndexPath *indexPathCell = [addFriendTableView indexPathForCell:clickedCell];

    NSLog(@"indexPathCell %i",(int)indexPathCell.row);

        Contacts* selectedContact = [contactsArray objectAtIndex:indexPathCell.row];

        if([self image:button.imageView.image isEqualTo:[UIImage imageNamed:@"addFriend.png"]]==YES){

    [button setImage:[UIImage imageNamed:@"addFriendPressed.png"] forState(UIControlStateNormal)];
     [selectFriendsArray addObject:selectedContact];
           counter++
        }

        else if( [self image:button.imageView.image isEqualTo:[UIImage imageNamed:@"addFriendPressed.png"]]==YES)
        {
            [button setImage:[UIImage imageNamed:@"addFriend.png"] forState:(UIControlStateNormal)];
            counter--;
            [selectFriendsArray removeObject:selectedContact];
            }

2条回答
你好瞎i
2楼-- · 2019-08-20 06:09

This is down to cell reuse. When cells scroll off screen, they are put into a queue to be reused as other cells appear on screen. The dequeue method pulls them off this queue. Your problem is that the button image does not get reset when this happens, so if it had previously been changed, it wiil remain.

You need to add some code to your cellForRowAtIndexPath method to set the correct image based on its row.

查看更多
Explosion°爆炸
3楼-- · 2019-08-20 06:27

Here what you are doing is getting same UIButton object for every cell. You need to create a separate button for every cell.

Identify this UIButton object with unique tag and then select that cell for further.

Create a tag for UIButton object. Put below line at top of your view controller class after #import statments

#define kButtonTag 1000

Now set unique tag for each object.

 static NSString *simpleTableIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
        NSLog(@"index path inside cell = nil %i",(int)indexPath);
    }

    // kButtonTag = 1000

     NSInteger btnTag = kButtonTag + indexPath.row;

     [[cell.contentView viewWithTag:btnTag] removeFromSuperview];

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // Set button properties which you required here...
    //....

    [button setTag:[cell viewWithTag:kButtonTag + indexPath.row]];
    [button addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchDown];

    [cell.contentView addSubview:button];

In your select method, check for that button tag.

- (void) select:(UIButton *)sender {
    NSInteger tag = sender.tag;
    NSInteger index = tag - kButtonTag;

    // Above index is unique index for your cell.
}

Hope you will get idea, From this short explanation.

查看更多
登录 后发表回答