Objective c UITableViewCellAccessoryCheckmark位于不同部分

Objective c UITableViewCellAccessoryCheckmark位于不同部分,objective-c,ios,uitableview,checkmark,Objective C,Ios,Uitableview,Checkmark,我在UITableView中有两个部分。我想在我选择的行上启用UITableViewCellAccessoryCheckmark 两个部分中只能有一个选中行 我该怎么做?我找到的示例仅显示了如何使用1节来完成此操作。 谢谢。您可以保留一个用于存储节号和行号的变量,以及一个用于记录所选内容的bool。在didSelectRow方法中,您可以访问这些值以删除上一个单元格中的选择(如果有),并将其更新为当前选定的行和节值。这可能是您可以帮助的。在UITAVLEVEW的委托方法中,您可以实现此代码 -

我在UITableView中有两个部分。我想在我选择的行上启用UITableViewCellAccessoryCheckmark
两个部分中只能有一个选中行
我该怎么做?我找到的示例仅显示了如何使用1节来完成此操作。


谢谢。

您可以保留一个用于存储节号和行号的变量,以及一个用于记录所选内容的bool。在
didSelectRow
方法中,您可以访问这些值以删除上一个单元格中的选择(如果有),并将其更新为当前选定的行和节值。

这可能是您可以帮助的。在UITAVLEVEW的委托方法中,您可以实现此代码

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==1)
    {
        NSUInteger row=indexPath.row;

//  implement you code  for first section;

    }
    elseif(indexPath.section==2)
    {
        NSUInteger row=indexPath.row;
//implement you code  second first section;

    }
.....................
.....................
.....................
}

这里我假设每个部分的行总数是3,部分总数是2

 - (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


       for(NSInteger i=0;i<2;i++) 
       {


        for (int index=0; index<3; index++) 
        {
            NSIndexPath *indexpath=[NSIndexPath indexPathForRow:index inSection:i];
            UITableViewCell *cell=[tableView1 cellForRowAtIndexPath:indexpath];

            if ([indexPath compare:indexpath] == NSOrderedSame) 
            {

                cell.accessoryType=UITableViewCellAccessoryCheckmark;
            }
            else
            {

                cell.accessoryType=UITableViewCellAccessoryNone;
            }
        }
       }
}
-(void)tableView:(UITableView*)tableView 1 didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{

对于(NSInteger i=0;i这将适用于每个部分中任意数量的部分和单元

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section]].accessoryType = UITableViewCellAccessoryCheckmark;
    [self deSelectOtherCellsInTableView:tableView Except:indexPath]; 
}

-(void)deSelectOtherCellsInTableView:(UITableView *)tableView Except:(NSIndexPath *)indexPath
{
    for(UITableViewCell *cell in [tableView visibleCells]){
        NSIndexPath *index = [tableView indexPathForCell:cell];
        if(index.section == indexPath.section && index.row != indexPath.row){
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}

谢谢你的回复(:上面的代码允许在每个部分中打勾,但是我只想在部分中打一个勾。嗨,我已经更新了我的答案,以前我以为你想在每个部分打一个勾。现在根据你的要求,这意味着在所有部分中只有一个勾。嗨,谢谢(:它可以工作。但是如果我在每个部分中有不同数量的行,它还会工作吗?是的,它可以工作,但请确保当前在内部循环中执行3次,bcoz每个部分中的行总数是3,这是最大值。如果您希望在每个部分中有不同数量的行,那么此代码没有问题,您只替换3(当前的行数)到所有部分中最大的行数,请尝试此操作。