禁用在didSelectRowAtIndexPath之后选择其他单元格

时间:2014-03-04 18:50:20

标签: ios xcode uitableview

我需要在我的应用程序中选择正确的单元格后禁用选择其他单元格 因此,如果用户选择了错误答案,他可以选择其他单元格,但如果选择正确答案,则他不能这样做 帮我解决我的问题。 我的代码是:

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger rowNumber = [indexPath row];
NSArray *array = [self getAnswers];
if (rowNumber == 0)
{
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (rowNumber == [array[array.count - 2] intValue])
{
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
    [_rightAnswersArray addObject:[NSNumber numberWithInt:_index+1]];
    NSLog(@"rightAnswersArray = %@",_rightAnswersArray);
    for (rowNumber = 1; rowNumber < array.count - 1; rowNumber++)
    {
        cell.userInteractionEnabled = NO;
    NSLog(@"%hhd",cell.userInteractionEnabled);
    }
}
else
{
    cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
    [_wrongAnswersArray addObject:[NSNumber numberWithInt:_index+1]];
    NSLog(@"wrongAnswersArray = %@",_wrongAnswersArray);
    [self showComment];
}
}

UPD:解决了这段代码。

if (rowNumber == [array[array.count - 2] intValue])
{
cell.contentView.backgroundColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
[_rightAnswersArray addObject:[NSNumber numberWithInt:_index+1]];
NSLog(@"rightAnswersArray = %@",_rightAnswersArray);
for (rowNumber = 1; rowNumber < array.count - 1; rowNumber++)
self.tableView.allowsSelection = NO;
}

1 个答案:

答案 0 :(得分:2)

您应该实施UITableViewDelegate方法

// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

看起来应该是这样的

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _isRightAnswerAlreadySelected ? nil : indexPath;
}
应在_isRightAnswerAlreadySelected

中设置

(void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath