0

I have a TableView with custom cells, selected radio button (image) is shown when selected. Everything works fine but when I start scrolling the cells aren't showing correctly (the unselected radio button (image) are placed in "selected" cells). Does anyone know how to fix this?

Please have a look in my code

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

    ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
NSString *tempTitle = [tempArr objectAtIndex:indexPath.row];
            [cell.titleLblOutlet setText:tempTitle];
            cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_inactive.png"];
            if (indexPath == self.selectedCellIndexPath) {
                 cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_active.png"];
             }
    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
     This works nicely but the only problem I found with this approach is that the user can deselect a row by tapping on it, leaving nothing selected. Simply add the following to prevent this: -(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath { return nil; }
     */
    for ( NSIndexPath* selectedIndexPath in tableView.indexPathsForSelectedRows ) {
        if ( selectedIndexPath.section == indexPath.section ) {
            [tableView deselectRowAtIndexPath:selectedIndexPath animated:NO] ;

            ProfileTableViewCell *cell = [tableView cellForRowAtIndexPath:selectedIndexPath];
            cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_inactive.png"];

        }

    }
    return indexPath ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

            if (indexPath != self.selectedCellIndexPath) {
                ProfileTableViewCell *tempPreviousCell = [tableView cellForRowAtIndexPath:self.selectedCellIndexPath];
                tempPreviousCell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_inactive.png"];

                self.selectedCellIndexPath = [tableView indexPathForSelectedRow];

                ProfileTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_active.png"];
            }
}

Thanks in Advance.

Lion
  • 872
  • 1
  • 17
  • 43
  • You have a cell-reuse issue, and it's been answered a zillion times (inexact count). – Avi Mar 21 '16 at 08:56
  • You forgot else condition in cellForRowAtIndexPath. Try this, if (indexPath == self.selectedCellIndexPath) { cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_active.png"]; } else { cell.radioBtnImgVwOutlet.image = [UIImage imageNamed:@"radio_inactive.png"]; } – Bharat Modi Mar 21 '16 at 09:01
  • @Avi [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath]; this is the code, I used. Could you please explained me how my question has been answered zillion times – Lion Mar 21 '16 at 09:43
  • This code is wrong: `if (indexPath != self.selectedCellIndexPath)`. Comparing pointers for equality is almost always wrong. Compare objects instead. – Avi Mar 21 '16 at 09:47

1 Answers1

2

Change your if condition in cellForRowAtIndexPath path.

    if (indexPath.row == self.selectedCellIndexPath.row && 
indexPath.section == self.selectedCellIndexPath.section)
Apurv
  • 17,116
  • 8
  • 51
  • 67