0

I am developing an app in which, the image of the UIButton in UITableViewCell should change on click and I have done this in a custom cell. Right now, I am able to change the image of the button but it is also changing the image of few other buttons too as I scroll down (as cellForRowAtIndexPath: is called on scrolling).

Here is the code.

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
        _cell = (ListTableViewCell *)[self.tblList dequeueReusableCellWithIdentifier:@"listTableViewCell"];
        if (_cell == nil) {
            _cell = [[ListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"listTableViewCell"];
        } else {
            _cell.imgIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_arrImages objectAtIndex:indexPath.row]]];
            _cell.lblList.text = [NSString stringWithFormat:@"%@",[_arrNames objectAtIndex:indexPath.row]];
            _cell.btnList.tag = indexPath.row;
            if (_cell.btnList.tag == indexPath.row) {
                [_cell.btnList addTarget:self action:@selector(btnPressedMethodCall:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        return _cell;
    }


- (void) btnPressedMethodCall:(UIButton*)sender  {
    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"red_image.png"] forState:UIControlStateSelected];
        [sender setSelected:NO];
    } else {
        [sender setImage:[UIImage imageNamed:@"black_image.png"] forState:UIControlStateNormal];
        [sender setSelected:YES];
    }
}

Could someone please tell how this problem can be solved. Any help is appreciated thanks.

Simeryn Denis
  • 168
  • 1
  • 10
  • Check this link [Tableview button click](https://stackoverflow.com/questions/28894765/uibutton-action-in-table-view-cell/41374087#41374087) – kalpesh Apr 02 '18 at 12:56
  • the tableview needs a datasource to display data. Once you change the button attributes, you need to update table datasource, so the tableview will display properly. – Teja Nandamuri Apr 02 '18 at 12:58
  • please reload uitableviewcell on your Action - btnPressedMethodCall – Akshay Degada Apr 02 '18 at 13:05
  • Set both states image initially and just call `[sender setSelected:NO/YES]` on button click. You will also need to handle this in `cellForRowAtIndexPath` as this methods calls on scrolling. – TheTiger Apr 02 '18 at 13:11

1 Answers1

2

Instead of changing image in button click event. Add selected button indexPath in NSMutableArray and in cellForRow method check NSMutableArray contain current indexPath. if yes than change button image else set normal image like below.

Swift

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = self.tblVW.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

    cell.selectionStyle = .none

    cell.btn.tag = indexPath.row
    cell.btn.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)

    if arrIndexPaths.contains(indexPath) {
        cell.btn.setImage(YOUR_BUTTON_SELECTED_IMAGE, for: .normal)
    }
    else {
        cell.btn.setImage(YOUR_BUTTON_DEFAULT_IMAGE, for: .normal)
    }
    cell.layoutSubviews()
    return cell;
}

@IBAction func btnTapped(_ sender: UIButton) {
    let selectedIndexPath = NSIndexPath.init(row: sender.tag, section: 0)
    arrIndexPaths.add(selectedIndexPath)
    self.tblVW.reloadData()
}

If you want to reload only single row than replace self.tblVW.reloadData() with self.tblVW.reloadRows(at: [selectedIndexPath as IndexPath], with: UITableViewRowAnimation.none)

Objective C

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"TableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil] objectAtIndex:0];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.btn.tag = indexPath.row
    [cell.btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

    if ([arrIndexPaths containsObject: indexPath]) {
        [cell.btn setImage:YOUR_BUTTON_SELECTED_IMAGE forState:UIControlStateNormal];
    }
    else {
        [cell.btn setImage:YOUR_BUTTON_DEFAULT_IMAGE forState:UIControlStateNormal];
    }

    [cell layoutSubviews];
    return cell;
}

-(IBAction)btnTapped:(UIButton *)sender {
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
    [arrIndexPaths addObject:selectedIndexPath];
    [self.tblVW reloadData];  // Reload Whole TableView

    //OR
    NSArray* indexArray = [NSArray arrayWithObjects:selectedIndexPath, nil];
    [self.tblVW reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone]; // Reload Single Row
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59