2017-07-12 2 views
-1

Ich habe die folgende Implementierung, wo Benutzer in der Lage, so viele Elemente wie er möchte auswählen kann.Häkchen bei tableView

Ich könnte Kontrollkästchen sehen, sobald Benutzer das Element auswählt, jedoch wenn Benutzer nach oben oder unten scrollen, gibt es ein seltsames Verhalten, weil ich sehe andere Elemente ausgewählt sind, auch wenn es nicht ausgewählt ist.

Zum Beispiel, wenn ich 3 Elemente in der Reihenfolge wähle, wenn ich nach unten scrolle, könnte ich das gleiche Muster (3 Elemente in der Reihenfolge) ausgewählt sehen. ist es etwas mit dequeueReusableCellWithIdentifier verbunden? Wenn ja, was mache ich falsch?

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    self.comboTableView.allowsMultipleSelection = YES; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return movies.count; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return ((Section*)movies[section]).movies.count ; 
} 

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

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    cell.textLabel.text = ((Section*)movies[indexPath.section]).movies[indexPath.row]; 
    return cell; 
} 
+0

dupliziert https://stackoverflow.com/questions/23727255/multiple-checkmark-when-row-selected-in-uitableview-ios – Bejibun

Antwort

2

UITableView Cells wiederverwendet werden, da Sie dequeueReusableCellWithIdentifier:cellIdentifier verwenden. Sie müssen also prüfen, ob das aktuelle Indexpfadelement ausgewählt ist oder nicht. Wenn ausgewählt, aktivieren Sie das Häkchen. deaktivieren Sie es sonst

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

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    NSArray <NSIndexPath*> *selectedIndexPaths = [tableView indexPathsForSelectedRows]; 

    if ([selectedIndexPaths containsObject:indexPath]) { 
      // Add the code of enable tick mark 
    } else { 
      // Add the code of disable tick mark 
    } 
} 
1

Dieses Problem liegt daran, dass Sie die Tabelle "viewviewcell" überprüfen, aber nicht Ihre Daten. Wenn Sie diese Überprüfungsinformationen nicht aufzeichnen, zeichnet Ihre Tabellenansichtszelle den Überprüfungsstatus auf, selbst wenn diese Zelleninstanz geändert wurde, um andere Daten mit unterschiedlichem indexPath anzuzeigen.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    ((Section*)movies[indexPath.section]).movies[indexPath.row].checked = true; 
} 
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    ((Section*)movies[indexPath.section]).movies[indexPath.row].checked = false; 
} 

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

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    cell.textLabel.text = ((Section*)movies[indexPath.section]).movies[indexPath.row]; 
    cell.accessoryType = ((Section*)movies[indexPath.section]).movies[indexPath.row].checked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 
    return cell; 
} 
+0

halte ich wirklich Spur jeder Auswahl in meiner Daten durch Hinzufügen einer anderen Variablen in meiner Modellklasse, oder lassen Sie einfach Tableview behandelt es für mich? Ich habe Ihre Mühe Upvoted – hotspring

+0

Ja, Sie sollten aufzeichnen, weil Tableview nicht für Sie behandelt. Deshalb hast du verwirrt. – Codus

+0

'[tableView indexPathsForSelectedRows]' speichert ausgewählte 'section' und' row' (indexPath), oder? – hotspring

Verwandte Themen