Ios 如何在objective c中限制用户按顺序选择单元格

Ios 如何在objective c中限制用户按顺序选择单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我是iOS新手,我面临一个关于如何选择单元格的问题。这意味着用户不能直接单击第三行,他应该选择第一行,然后选择第二行和第三行。有可能吗 我试试这个代码 tableView.userInteractionEnabled = NO; 但是使用这段代码我不能选择任何单元格,我有一个来自web服务的动态数组。提前谢谢 您应该保存最后一行indexath.row,以比较当前选定的行。下面的代码强制用户按顺序选择行:用户必须选择行0->行1->行2 NSIndexPath *_lastIndexPath

我是iOS新手,我面临一个关于如何选择单元格的问题。这意味着用户不能直接单击第三行,他应该选择第一行,然后选择第二行和第三行。有可能吗

我试试这个代码

tableView.userInteractionEnabled = NO;

但是使用这段代码我不能选择任何单元格,我有一个来自web服务的动态数组。提前谢谢

您应该保存最后一行
indexath.row
,以比较当前选定的行。下面的代码强制用户按顺序选择行:用户必须选择行0->行1->行2

NSIndexPath *_lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSMutableArray *_selectedIndexPaths = [[NSMutableArray alloc] init];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_selectedIndexPaths containsObject:indexPath]) {
        // Set the color for selected cell
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_lastIndexPath.row + 1 >= indexPath.row) {
        // Implement what you want
    } else {
        // You should show the introduction of order operation for user
        _lastIndexPath = indexPath;
    }
    if (![_selectedIndexPaths containsObject:indexPath]) { // Check if array does not contain selecting cell, add this cell to array
        [_selectedIndexPaths addObject:indexPath];
        [tableView reloadData];
    }
}

NSMutableArray

NSMutableArray *arr_selected_indexs = [NSMutableArray alloc]init];
进入
tableview
cellforrowatinexpath
方法

[arr addObject: indexPath.row +1 ];
[self.tableView reloaddata];
检查

if indexPath == 0
{
cell.userInteractionenabled = YES;
cell.contentView.backgroundColor = [UIColor greenColor];
 }else if [arr containsObject : indexPath.row]
{

cell.userInteractionenabled = YES;
cell.contentView.backgroundColor = [UIColor greenColor];
}else
{
 cell.userInteractionenabled = NO;
cell.contentView.backgroundColor = [UIColor redColor];
}
现在进入
didselectrowatinexpath
方法

[arr addObject: indexPath.row +1 ];
[self.tableView reloaddata];
使用

对于特定的选择行,只需通过

return NO;

@Jecky你怎么能分享一些代码呢请记录最后一个索引,如果不是下一个索引,请取消选中该行@Tj3n@Jecky请分享。我曾经和NSMutableArray合作过,但我觉得你的比我好。因此,感谢您提供这个@MujuWhat is _lastIndexPath是它的最后一个索引存储值。是的,它存储用户可以选择的最后一个NSIndexPath。但是在何处声明此行NSIndexPath*_lastIndexPath=[NSIndexPath indexPathForRow:0第0节:0];如果_lastIndexPath.row=0(第一行),则用户无法选择第二行。这是主要的想法。你应该把它放在
viewDidLoad
中,或者当你创建TableView时(如果你是通过编码创建TableView的)。我可以用同样的方法更改单元格颜色吗。与所有选定单元格一样,所有选定单元格都设置为绿色,未选定索引都设置为白色或红色。@Muju我根据您的需要更新了我的代码。请检查它请查看nynohu答案_lastIndexPath.row+1不是递增的。请帮助。@Muju您将尝试我的所有索引的答案。虽然我会尝试上面的答案,并让你知道