-1

I'm a newbie with iOS programming language. I try to use table with button in each row.

  1. when i click button in the first cell, it work pretty well.
  2. but when i scroll down, the cell that i don't click also appear click.

I want to show the clicked button in only cell(s) that I clicked? Code:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath{
ClassifyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if ([[[arrayResult objectAtIndex:indexPath.row] objectForKey:@"COLABO_FLD_SRNO"] isEqualToString:@"I"]) {
    cell.editButton.hidden = YES;
    cell.folder_name.text = [[arrayResult objectAtIndex:indexPath.row] objectForKey:@"COLABO_FLD_NM"];
}else{
    cell.editButton.hidden = NO;
    cell.folder_name.text = [[arrayResult objectAtIndex:indexPath.row] objectForKey:@"COLABO_FLD_NM"];
    cell.editButton.tag = indexPath.row;
    [cell.editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.checkButton.tag = indexPath.row;
    [cell.checkButton addTarget:self action:@selector(checkButtonActoin:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;

}

-(void)checkButtonActoin:(UIButton *)sender{
if (sender.selected == YES) {
    sender.selected = NO;
}else if(sender.selected == NO){
    sender.selected = YES;
}else {
    sender.selected = NO;
}

}

vichhai
  • 2,548
  • 2
  • 15
  • 25

1 Answers1

0

UITableView reuse the state of cell's subviews for new cells. To prevent reusing just configure your subviews explisitly in tableView:cellForRowAtIndexPath:. In your case you need to set selected property of your button to YES, if it was selected, and to NO, if it was not selected.

Add NSMutableArray property to your view controller:

@property (strong, nonatomic) NSMutableArray *arrayForSelectedIndexPaths;

Change your tap method:

-(void)checkButtonAction:(UIButton *)sender{
    UITableViewCell *cell = (UITableViewCell *) sender.superview.superview...; //find appropriate number of superviews to get your cell
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if (sender.selected) {
        sender.selected = NO;
        [self.arrayForSelectedIndexPaths removeObject:indexPath];
    }else {
        sender.selected = YES;
        [self.arrayForSelectedIndexPaths addObject:indexPath];
    }
}

Then in tableView:cellForRowAtIndexPath: add this line:

cell.checkButton.selected = [self.arrayForSelectedIndexPaths containsObject:indexPath];
Vlad
  • 7,199
  • 2
  • 25
  • 32
  • After apply this one, I don't have duplicated selected in each cell. – vichhai Apr 17 '15 at 08:17
  • After apply this one, I don't have a duplicated selected button. But when i scroll up and down, the button that I already selected becomes unselected. – vichhai Apr 17 '15 at 08:19
  • Yes. You need to provide some logic to remember cell's with selected buttons. – Vlad Apr 17 '15 at 08:20
  • Ok. let me try. Thank you. – vichhai Apr 17 '15 at 08:22
  • For example add an `NSMutableArray` property to your view controller, add here indexPath for cell when button was selected and remove it when your button was unselected. Then in `tableView:cellForRowAtIndexPath:` paste this cell.checkButton.selected = `[self.arrayForSelectedIndexPaths contains indexPath]` – Vlad Apr 17 '15 at 08:23
  • Don't forget about finding a right number of superviews to get a cell. It could be `sender.superview.superview` or `sender.superview.superview.superview` if you button lies on other cell's subview. – Vlad Apr 17 '15 at 09:45