我试图在一个部分中一次只检查一个 UITableViewCell 。我要做的是取消选中已选中的 UITableViewCell (如果有的话)并检查当前选定的单元格。所以基本上,如果选择了一个 CellA,我选择了 CellB,我希望 CellA 取消选择,CellB 选择。
这是我为尝试实现这一目标所做的工作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(![self.selectedIndexPaths containsObject:indexPath]) {
[self.selectedIndexPaths addObject:indexPath];
}
else {
[self.selectedIndexPaths removeObject:indexPath];
}
if (indexPath.section == 0) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else if (indexPath.section == 1) {
if (![self.selectedIndexPaths containsObject:indexPath]) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 1) {
[tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES;
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
else if (indexPath.section == 2) {
if (![self.selectedIndexPaths containsObject:indexPath]) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 2) {
[tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES;
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if ([tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
}
}
else if (indexPath.section == 1) {
if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 1 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
else if (indexPath.section == 2) {
if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 2 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
}
如您所见,我有三个部分,第一个部分用户可以选择他们想要的任意多个单元格,但在第二个和第三个部分中,它仅限于一个。
问题:每当我选择一个单元格,然后选择另一个单元格时,它就可以工作。如果我选择第一个单元格,现在它们都被选中。
我将不胜感激任何帮助或建议。谢谢!