user6694170
user6694170

Reputation:

UIButton in UITableView clicks on multiple buttons in objective c

I have 2 buttons in UITableview. When i select one button in my UITableView other buttons lower down are also clicked. Why is this clicking one button selecting multiple UIButtons?

I am using storyboard and my code is:

- (IBAction)yesBtnAction:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [self.take5table indexPathForCell:clickedCell];
    UIButton *yesBTN=(UIButton *)[clickedCell.contentView viewWithTag:100];
    UIButton *noBTN=(UIButton *)[clickedCell.contentView viewWithTag:111];
    [yesBTN setBackgroundColor:[UIColor colorWithRed:0.23 green:0.62 blue:0.23 alpha:1.0]];
    [noBTN setBackgroundColor:[UIColor whiteColor]];
}

- (IBAction)noBtnAction:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [self.take5table indexPathForCell:clickedCell];
    UIButton *yesBTN=(UIButton *)[clickedCell.contentView viewWithTag:100];
    UIButton *noBTN=(UIButton *)[clickedCell.contentView viewWithTag:111];
    [yesBTN setBackgroundColor:[UIColor whiteColor]];
    [noBTN setBackgroundColor:[UIColor redColor]];
}

Any help/suggestions would be greatly appreciated.

Upvotes: 0

Views: 675

Answers (3)

Sushil Vyas
Sushil Vyas

Reputation: 70

I think this code is helpful for u

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"" forIndexPath:indexPath];
     [cell.buttnName1 addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.buttnName2 addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return Cell;
    }
//action for button

        - (void)BtnAction:(id)sender//type 1 button action
        {
        UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
           UIButton *button = sender;

            for (button in clickedCell.ButtonArr)//button create IBOtletcollections
            {
                [button setSelected:([button isEqual:sender])?YES:NO];

                if ([button isSelected]) {

                    [button setBackgroundImage:[UIImage imageNamed:@"select_toggle"] forState:UIControlStateSelected];
                    tagType = button.tag; 
    //use local variable Tagtype(NsIntiger)  
                }else{
                    [button setBackgroundImage:[UIImage imageNamed:@"unselect_toggle"] forState:UIControlStateNormal];
                }
            }



        }

Upvotes: -1

rmaddy
rmaddy

Reputation: 318774

The problem is that you are not properly handling cell reuse.

You need to make sure your cellForRowAtIndexPath properly sets the state of each button. This needs to be based on some state you keep track of in your data model. This data model needs to be updated as each button it clicked.

You are attempting to keep the state in the button. That doesn't work.

Upvotes: 3

Rikh
Rikh

Reputation: 4222

I don't think its because of the same function being called. Your problem is owing to dequeuing of the cells. Example: In the first cell, when you click a button, you are changing the yesBTN to another color. So when the cell is re-used you are using that same cell with the changed color. What you could try is:

Add the indexPath.row of all the buttons clicked to a NSMutableArray and in your cellForRowAtIndexPath check if the array contains that particular indexPath.row

Upvotes: 1

Related Questions