2017-07-15 5 views
0

Ich habe ein Problem in der folgenden Implementierung. Ich definiere, wie viele maximale Elemente in jedem Abschnitt in der Tabellenansicht ausgewählt werden können.Limit-Nummer des Elements kann in jedem Abschnitt ausgewählt werden

des Benutzers Nehmen wir an, nur zwei Artikel im Abschnitt 0 wählen erlaubt Wenn Benutzer die dritte eine, dann die erste ausgewählte Element abgewählt und dritte Element wird Häkchen wird auszuwählen versucht haben Zubehörteil.

Meine folgende Implementierung kann nicht umgehen, es ermöglicht mehr als zwei Elemente mit allen Häkchen. Ich frage mich, wo ich falsch liege?

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    int numberOfItemsSelected = [[selectedRowsInSectionDictionary valueForKey: [NSString stringWithFormat:@"%ld",indexPath.section]] intValue]; 

    if(((ComboItem*)comboItemsArray[indexPath.section]).itemNumberEachCombo == numberOfItemsSelected) 
    { 
     NSIndexPath *oldIndex = [self.comboTableView indexPathForSelectedRow]; 
     [self.comboTableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone; 
     [self.comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
     return nil; 
    } 
    else 
    { 
     return indexPath; 
    } 
} 

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

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

- (void)selectedItem: (UITableView *)tableView { 
    selectedRowsInSectionDictionary = [NSMutableDictionary dictionary]; 
    NSArray <NSIndexPath*> *selectedIndexPaths = [tableView indexPathsForSelectedRows]; 
    for (NSIndexPath *indexpath in selectedIndexPaths) { 
     NSString *sectionKey = [NSString stringWithFormat:@"%ld",indexpath.section]; 
     NSInteger numberOfSelectedRows = [[selectedRowsInSectionDictionary objectForKey: sectionKey] integerValue]; 
     numberOfSelectedRows++; 
     [selectedRowsInSectionDictionary setObject:@(numberOfSelectedRows) forKey: sectionKey]; 
    } 
} 

-(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]; 
    } 
    if([[tableView indexPathsForSelectedRows] containsObject:indexPath]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    cell.textLabel.text = ((ComboItem*)comboItemsArray[indexPath.section]).allComboItems[indexPath.row]; 
    return cell; 
} 
+0

cellForRowAtIndexPath Methode hinzufügen. – KKRocks

+0

habe ich gerade aufgenommen. – hotspring

Antwort

2

EDIT: (nachdem es auf meinem Beispielprojekt versucht)

Implementierung von willSelectRowAtIndexPath: Methode sieht falsch zu mir. Außerdem gibt indexPathForSelectedRow das erste Indexpfadobjekt im Array der Zeilenauswahl zurück. daher immer den ersten ausgewählten Index des 0. Abschnitts.

Dies ist die Umsetzung nach Entfernen Methode - (void)selectedItem: (UITableView *)tableView und selectedRowsInSectionDictionary variable und Hinzufügen ein neues Wörterbuch NSMutable Variable für ausgewählte Indexpfade beibehalten Array selectedIndexPathsInSectionDictionary.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    selectedIndexPathsInSectionDictionary = [NSMutableDictionary dictionary]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     int numberOfItemsSelected = [[selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)] count]; 

     if(((ComboItem*)comboItemsArray[indexPath.section]).itemNumberEachCombo == numberOfItemsSelected) 
     { 
      NSIndexPath *oldIndex = [[selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)] firstObject]; 
      [tableView deselectRowAtIndexPath:oldIndex animated:NO]; 
      [self tableView:tableView didDeselectRowAtIndexPath:oldIndex]; 
     } 
     return indexPath; 
    } 

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

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

    - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath { 
     NSMutableArray* array = [selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)]; 
     if(array){ 
      [array addObject:indexPath]; 
     } else { 
      array = [NSMutableArray array]; 
      [array addObject:indexPath]; 
      [selectedIndexPathsInSectionDictionary setObject:array forKey:@(indexPath.section)]; 
     } 
    } 

    - (void)deselectItemAtIndexPath:(NSIndexPath*)indexPath { 
     NSMutableArray* array = [selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)]; 
     [array removeObject:indexPath]; 
    } 

enter image description here

github link der Probe Projekt

+0

Wenn der zweite Abschnitt die maximale Anzahl erreicht, werden die Elemente des ersten Abschnitts aus bestimmten Gründen ebenfalls deaktiviert. Es ist seltsam. Irgendeine Idee? – hotspring

+0

Ja, ich habe es, indexPathForSelectedRow gibt immer das erste Indexpfad-Objekt im Array der Zeilenauswahl zurück; daher wird der erste ausgewählte Index des 0-ten Abschnitts immer zurückgegeben, was die Logik fehlerhaft macht. besser wäre es, ein Verzeichnis ausgewählter Indexpfade mit Schlüssel als Abschnitt und Array von Indexpfaden als Wert zu pflegen. –

+0

Könnten Sie bitte Ihre Antwort hinzufügen? Ich habe diese Logik bereits gehabt (verfolgen Sie den gewählten indexPath für jeden Abschnitt im Wörterbuch, '' (void) selectedItem: (UITableView *) tableView' aber wie würde ich es verwenden? – hotspring

Verwandte Themen