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;
}
cellForRowAtIndexPath Methode hinzufügen. – KKRocks
habe ich gerade aufgenommen. – hotspring