2016-03-21 15 views
0

Ich habe eine TableView mit benutzerdefinierten Zellen, ausgewählte Radio-Button (Bild) wird angezeigt, wenn ausgewählt. Alles funktioniert gut, aber wenn ich mit dem Scrollen anfange, werden die Zellen nicht korrekt angezeigt (der nicht ausgewählte Radioknopf (Bild) wird in "ausgewählte" Zellen platziert). Kann jemand das beheben?Scrollen von UITableview ändern Sie das Bild, UITableview Scroll-Problem

Bitte schauen Sie in meinem 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"]; 
      } 
} 

Vielen Dank im Voraus.

+0

Sie haben ein Problem mit der Wiederverwendung von Zellen, und es wurde unzählige Male beantwortet (ungenaue Anzahl). – Avi

+0

Sie haben in cellForRowAtIndexPath eine andere Bedingung vergessen. Versuchen Sie dies, wenn (indexPath == self.selectedCellIndexPath) { cell.radioBtnImgVwOutlet.image = [UIImage imageNamed: @ "radio_active.png"]; } sonst {cell.radioBtnImgVwOutlet.image = [UIImage imageNamed: @ "radio_inactive.png"]; } –

+0

@Avi [tableView dequeueReusableCellWithIdentifier: cellidentifier forIndexPath: indexPath]; Das ist der Code, den ich benutzt habe. Könnten Sie mir bitte erklären, wie meine Frage zigmal beantwortet wurde? – Lion

Antwort

2

Ändern Sie Ihre if-Bedingung in cellForRowAtIndexPath Pfad.

if (indexPath.row == self.selectedCellIndexPath.row && 
indexPath.section == self.selectedCellIndexPath.section) 
+0

vielen Dank Herr – Lion

Verwandte Themen