古怪的 UITableViewCellAccessoryCheckmark 行为

Wacky UITableViewCellAccessoryCheckmark Behavior

我正在以编程方式设置 UITableViewCellAccessoryCheckmark,如下面的代码所示。有时它工作得很好,包含应该检查的标题的单元格被选中,而不检查不应该检查的单元格。但是在其他时候,当一个单元格收到支票时,tableview 会向另一个不应检查的单元格添加一个随机复选标记。但这只发生在某些列表上,即使它可能包含与另一个工作正常的列表完全相同的数据。这些单元格包含在一个最多可能有 6 行的部分中,因此一旦您滚动到该部分,它们就会全部出现在屏幕上。大多数情况下,该部分只有 3 行。如果复选标记因为重复使用单元格而被搞砸,那么我认为有 6 行的部分会导致问题,但事实并非如此。混乱的部分是只有 3 行的部分!正如您将看到的,我已经记录了几乎每一行,调试器给了我我期望看到的内容,但是 tableview 只是添加了调试器中没有显示的额外复选标记。我只是不知道我做错了什么。如果有人能给我一个起点,让我知道还有什么要检查的,我会洗耳恭听。此刻我泪流满面!如果您需要任何其他代码,请告诉我。提前致谢!

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.section == 0 && indexPath.row == 0)
{
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

if(_book.hasNovellaArray)
{
    NSMutableArray *storyList = [[NSMutableArray alloc] init];
    NSMutableArray *newList = [[NSMutableArray alloc] init];

    for (id title in _book.novellaArray)
    {
        [storyList addObject:title];
        NSLog(@"added title in the collection:%@:",storyList);
    }

    for (id story in _book.novellaRead)
    {
        [newList addObject:story];
        NSLog(@"added story has been read:%@:", newList);
    }

    for (id i in storyList)
    {
        for (id j in newList)
        {
            if ([ i isEqualToString:j])
            {
                NSLog(@"%@ = %@", i, j);
                if ([_detailCell.detailLabel.text isEqualToString:i])
                {
                    _detailCell.accessoryType = UITableViewCellAccessoryCheckmark;
                    NSLog(@"cells have been checked");
                    NSLog(@"title checked: %@", _detailCell.detailLabel.text);
                    NSLog(@"at index path %ld", (long)indexPath.row);
                }
            }
        }
    }
}

}

细胞得到重复利用。当您有条件地设置单元格的 属性 时,您必须始终为所有其他条件重置 属性。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    } else {
        [cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
    }

    _detailCell.accessoryType = UITableViewCellAccessoryNone;

    if (_book.hasNovellaArray)
    {
        NSMutableArray *storyList = [[NSMutableArray alloc] init];
        NSMutableArray *newList = [[NSMutableArray alloc] init];

        for (id title in _book.novellaArray)
        {
            [storyList addObject:title];
            NSLog(@"added title in the collection:%@:",storyList);
        }

        for (id story in _book.novellaRead)
        {
            [newList addObject:story];
            NSLog(@"added story has been read:%@:", newList);
        }

        for (id i in storyList)
        {
            for (id j in newList)
            {
                if ([ i isEqualToString:j])
                {
                    NSLog(@"%@ = %@", i, j);
                    if ([_detailCell.detailLabel.text isEqualToString:i])
                    {
                        _detailCell.accessoryType = UITableViewCellAccessoryCheckmark;
                        NSLog(@"cells have been checked");
                        NSLog(@"title checked: %@", _detailCell.detailLabel.text);
                        NSLog(@"at index path %ld", (long)indexPath.row);
                    }
                }
            }
        }
    }
}