Iphone 在UITableView中为每个节设置一个复选标记并保存其状态

Iphone 在UITableView中为每个节设置一个复选标记并保存其状态,iphone,savestate,uitableview,checkmark,Iphone,Savestate,Uitableview,Checkmark,花了好几个小时也没用。我不知道我是否在搜索(谷歌搜索)方面没有效率,或者是因为在这方面的问题较少,或者我可能在执行专家的答案时犯了一些错误 我知道在一个部分中的一行设置附件类型复选标记,而在其他行设置无复选标记时,有几个问题,可以追溯到帖子和文档 我的表格视图中有2个部分。默认情况下,我希望选择每个部分中的第一行,即带有附件视图复选标记。现在,用户选择一行后,我希望复选标记仅在选定行上可见。我已尝试声明两个索引路径以跟踪每个节中的行选择。以下是我的实现代码: -(UITableViewCell

花了好几个小时也没用。我不知道我是否在搜索(谷歌搜索)方面没有效率,或者是因为在这方面的问题较少,或者我可能在执行专家的答案时犯了一些错误

我知道在一个部分中的一行设置附件类型复选标记,而在其他行设置无复选标记时,有几个问题,可以追溯到帖子和文档

我的表格视图中有2个部分。默认情况下,我希望选择每个部分中的第一行,即带有附件视图复选标记。现在,用户选择一行后,我希望复选标记仅在选定行上可见。我已尝试声明两个索引路径以跟踪每个节中的行选择。以下是我的实现代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.row,indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (indexPath.section == 0)
    {
        if(self.firstSectionIndex == indexPath)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

    if (indexPath.section == 1)
    {
        if(self.secondSectionIndex == indexPath)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];

        switch (indexPath.section)
        {
            case 0:
            {
                if (indexPath.row == 0)
                {
                    if(self.firstSectionIndex != indexPath)
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    }
                    else
                    {
                        cell.accessoryType = UITableViewCellAccessoryNone;
                    }
                    cell.textLabel.text = @"Yes";
                }
                if (indexPath.row == 1)
                {
                    cell.textLabel.text = @"No";
                }
            }
                break;
            case 1:
            {
                if (indexPath.row == 0)
                {
                    if(self.secondSectionIndex != indexPath)
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    }
                    else
                    {
                        cell.accessoryType = UITableViewCellAccessoryNone;
                    }
                    cell.textLabel.text = @"Easy";
                }
                if (indexPath.row == 1)
                {
                    cell.textLabel.text = @"Medium";
                }
                if (indexPath.row == 2)
                {
                    cell.textLabel.text = @"Hard";
                }
            }
                break;

            default:
                break;
        }
    }

    tableView.backgroundColor = [UIColor clearColor];
    tableView.backgroundView = nil;

    return cell;
}
在didSelectRowAtIndexPath中,我完成了以下操作:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
      self.firstSectionIndex = indexPath;
    }
    if (indexPath.section == 1)
    {
       self.secondSectionIndex = indexPath;
    }
    [tableView reloadData];
}
我搜索了要保存以供长期参考的单元格选择状态,在搜索过程中,我找到了一些有用的链接

但它正在选择多个单元格,并且附件类型复选标记正在应用于部分中的所有行。我不明白出了什么问题。请任何人指导我


提前感谢:)

您可以使用下面的代码实现:-

已编辑

.h文件

NSMutableArray *firstSelectedCellsArray;
NSMutableArray *secondSelectedCellsArray;
NSMutableArray *ThirdSelectedCellsArray;
在.m文件中

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [firstSelectedCellsArray containsObject:rowNumber]  )
            {
                [firstSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [firstSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [firstSelectedCellsArray containsObject:rowNumber]  )
            {
                [firstSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [firstSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    if (indexPath.section == 1)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [secondSelectedCellsArray containsObject:rowNumber]  )
            {
                [secondSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [secondSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [secondSelectedCellsArray containsObject:rowNumber]  )
            {
                [secondSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [secondSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if (indexPath.section == 2)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [ThirdSelectedCellsArray containsObject:rowNumber]  )
            {
                [ThirdSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [ThirdSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [ThirdSelectedCellsArray containsObject:rowNumber]  )
            {
                [ThirdSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [ThirdSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

}
现在在CellForRowatineXpath中放置一段代码:-

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    if(indexPath.section==0)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [firstSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if(indexPath.section==1)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [secondSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if(indexPath.section==2)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [ThirdSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    return cell;
}
演示链接

屏幕截图:


您可以使用下面的代码实现:-

已编辑

.h文件

NSMutableArray *firstSelectedCellsArray;
NSMutableArray *secondSelectedCellsArray;
NSMutableArray *ThirdSelectedCellsArray;
在.m文件中

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [firstSelectedCellsArray containsObject:rowNumber]  )
            {
                [firstSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [firstSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [firstSelectedCellsArray containsObject:rowNumber]  )
            {
                [firstSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [firstSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    if (indexPath.section == 1)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [secondSelectedCellsArray containsObject:rowNumber]  )
            {
                [secondSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [secondSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [secondSelectedCellsArray containsObject:rowNumber]  )
            {
                [secondSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [secondSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if (indexPath.section == 2)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [ThirdSelectedCellsArray containsObject:rowNumber]  )
            {
                [ThirdSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [ThirdSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [ThirdSelectedCellsArray containsObject:rowNumber]  )
            {
                [ThirdSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [ThirdSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

}
现在在CellForRowatineXpath中放置一段代码:-

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    if(indexPath.section==0)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [firstSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if(indexPath.section==1)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [secondSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if(indexPath.section==2)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [ThirdSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    return cell;
}
演示链接

屏幕截图:


用于实施复选标记

  • 为所选索引保留索引数组
  • 在cellFor行方法中,检查 选定的索引数组
  • 如果是真的

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    否则

  • 如果选中,则在didSelect方法中。同时从数组中删除索引 如所设置的附件类型无
  • 如果没有,则添加复选标记并将索引添加到数组中
  • 编辑

    NSMutableArray *firstSelectedCellsArray;
    NSMutableArray *secondSelectedCellsArray;
    NSMutableArray *ThirdSelectedCellsArray;
    

    在这种情况下,您需要的是每次选择一个单元格时的单个选择—从数组中删除所有索引并仅添加该索引,然后重新加载tableview以实现选中标记

  • 为所选索引保留索引数组
  • 在cellFor行方法中,检查 选定的索引数组
  • 如果是真的

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    否则

  • 如果选中,则在didSelect方法中。同时从数组中删除索引 如所设置的附件类型无
  • 如果没有,则添加复选标记并将索引添加到数组中
  • 编辑

    NSMutableArray *firstSelectedCellsArray;
    NSMutableArray *secondSelectedCellsArray;
    NSMutableArray *ThirdSelectedCellsArray;
    

    如果您每次选择一个单元格时都需要一次选择,请从数组中删除所有索引并仅添加该索引,然后重新加载tableview

    我找到了一个精确而完美的解决方案,感谢Lithu T.V和Nithin Gobel为我指明了正确的方向。不幸的是,他们的解决方案没有帮助我实现每个节1个复选标记,但事实上有多个复选标记。因此,我考虑以用户默认值保存所选行,并在最初选择每个节的第一行时,声明第一和第二节索引,分配到索引路径,然后分配单元格附件视图作为复选标记。现在,让我们首先处理cellForRowAtIndexPath:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.row,indexPath.section];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.textColor = [UIColor whiteColor];
            NSUserDefaults *savedState = [NSUserDefaults standardUserDefaults];
    
            switch (indexPath.section)
            {
                case 0:
                {
                    NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
                    if([[savedState objectForKey:@"firstSectionIndex"] isEqual: indexNumber])
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        self.firstSectionIndex = indexPath;
                    }
    
                    if (indexPath.row == 0)
                    {                    
                        NSObject * checkedCellObject = [savedState objectForKey:@"firstSectionIndex"];
                        if(checkedCellObject == nil)
                        {
                            self.firstSectionIndex = indexPath;
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        }
                        cell.textLabel.text = @"Yes";
                    }
                    if (indexPath.row == 1)
                    {
                        cell.textLabel.text = @"No";
                    }
                }
                    break;
                case 1:
                {
                    NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
                    if([[savedState objectForKey:@"secondSectionIndex"] isEqual: indexNumber])
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        secondSectionIndex = indexPath;
                    }
    
                    if (indexPath.row == 0)
                    {
                        NSObject * checkedCellObject = [savedState objectForKey:@"secondSectionIndex"];
                        if(checkedCellObject == nil)
                        {
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                            secondSectionIndex = indexPath;
                        }
                        cell.textLabel.text = @"Easy";
                    }
                    if (indexPath.row == 1)
                    {
                        cell.textLabel.text = @"Medium";
                    }
                    if (indexPath.row == 2)
                    {
                        cell.textLabel.text = @"Hard";
                    }
                }
                    break;
    
                default:
                    break;
            }
        }
    
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView = nil;
    
        return cell;
    }
    
    请注意,在每种情况下,我们都在检查选中的状态是否为保存的用户默认值,如果为零,我们检查每个部分的第一个单元格(行),这里我们使用表视图委托方法,是否在索引路径处选择行:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        if (indexPath.section == 0)
        {
            UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
            if(firstSectionIndex && firstSectionIndex != indexPath)
            {
                UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:firstSectionIndex];
                uncheckCell.accessoryType = UITableViewCellAccessoryNone;
            }
            self.firstSectionIndex = indexPath;
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:firstSectionIndex.row] forKey:@"firstSectionIndex"];
            checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    
        if (indexPath.section == 1)
        {
            UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
            if(secondSectionIndex)
            {
                UITableViewCell *unchekCell = [tableView cellForRowAtIndexPath:secondSectionIndex];
                unchekCell.accessoryType = UITableViewCellAccessoryNone;
            }
            self.secondSectionIndex = indexPath;
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:secondSectionIndex.row] forKey:@"secondSectionIndex"];
            checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    
    就这样,我们成功地完成了每个部分的一个复选标记,并且还将单元格的附件状态保存在用户默认值中,以供长期参考


    希望这对某些人有所帮助,谢谢:)

    我找到了一个精确而完美的解决方案,感谢Lithu T.V和Nithin Gobel为我指引了正确的方向。不幸的是,他们的解决方案并没有帮助我在每个部分中获得1个复选标记,但事实上有多个复选标记。所以我想用用户默认值保存所选行,并在最初选择每个节的第一行时,声明第一和第二节索引,分配给索引路径,然后分配单元格附件视图作为复选标记。现在,让我们先处理cellForRowAtIndexPath:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.row,indexPath.section];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.textColor = [UIColor whiteColor];
            NSUserDefaults *savedState = [NSUserDefaults standardUserDefaults];
    
            switch (indexPath.section)
            {
                case 0:
                {
                    NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
                    if([[savedState objectForKey:@"firstSectionIndex"] isEqual: indexNumber])
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        self.firstSectionIndex = indexPath;
                    }
    
                    if (indexPath.row == 0)
                    {                    
                        NSObject * checkedCellObject = [savedState objectForKey:@"firstSectionIndex"];
                        if(checkedCellObject == nil)
                        {
                            self.firstSectionIndex = indexPath;
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        }
                        cell.textLabel.text = @"Yes";
                    }
                    if (indexPath.row == 1)
                    {
                        cell.textLabel.text = @"No";
                    }
                }
                    break;
                case 1:
                {
                    NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
                    if([[savedState objectForKey:@"secondSectionIndex"] isEqual: indexNumber])
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        secondSectionIndex = indexPath;
                    }
    
                    if (indexPath.row == 0)
                    {
                        NSObject * checkedCellObject = [savedState objectForKey:@"secondSectionIndex"];
                        if(checkedCellObject == nil)
                        {
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                            secondSectionIndex = indexPath;
                        }
                        cell.textLabel.text = @"Easy";
                    }
                    if (indexPath.row == 1)
                    {
                        cell.textLabel.text = @"Medium";
                    }
                    if (indexPath.row == 2)
                    {
                        cell.textLabel.text = @"Hard";
                    }
                }
                    break;
    
                default:
                    break;
            }
        }
    
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView = nil;
    
        return cell;
    }
    
    请注意,在每种情况下,我们都在检查选中的状态是否为保存的用户默认值,如果为零,我们检查每个部分的第一个单元格(行),这里我们使用表视图委托方法,是否在索引路径处选择行:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        if (indexPath.section == 0)
        {
            UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
            if(firstSectionIndex && firstSectionIndex != indexPath)
            {
                UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:firstSectionIndex];
                uncheckCell.accessoryType = UITableViewCellAccessoryNone;
            }
            self.firstSectionIndex = indexPath;
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:firstSectionIndex.row] forKey:@"firstSectionIndex"];
            checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    
        if (indexPath.section == 1)
        {
            UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
            if(secondSectionIndex)
            {
                UITableViewCell *unchekCell = [tableView cellForRowAtIndexPath:secondSectionIndex];
                unchekCell.accessoryType = UITableViewCellAccessoryNone;
            }
            self.secondSectionIndex = indexPath;
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:secondSectionIndex.row] forKey:@"secondSectionIndex"];
            checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    
    就这样,我们成功地完成了每个部分的一个复选标记,并且还将单元格的附件状态保存在用户默认值中,以供长期参考


    希望这能帮助一些人,谢谢:)我注意到你从来没有清除过
    self.firstSectionIndex
    self.secondSectionIndex
    ,这不可能是正确的?@LithuT.V编辑:),你能帮忙吗me@trojanfoe您的意思是说将firstSectionIndex和secondSectionIndex设置为零??是;在设置一个的代码中,您不需要取消设置另一个吗?谷歌搜索而不是目瞪口呆:)我注意到您从未清除过
    self.firstSectionIndex
    self.secondSectionIndex
    ,这不可能是正确的?@LithuT.V Edited:),您能帮忙吗me@trojanfoe您的意思是说将firstSectionIndex和secondSectionIndex设置为零??是;在设置其中一个的代码中,您不需要取消设置另一个吗?非常感谢您的回答,但是在这两个部分的第一行中首先设置复选标记如何。我还观察到,在您的代码中,没有使用NSIndexPath*checkedIndexPath。是的,您是对的,很抱歉它是未使用的变量。所以当你的检查部分虎钳时,你必须把不同的选定数组放在一起,例如你必须检查FIR。