我正在以编程方式设置UITableViewCellAccessoryCheckmark,如下面的代码所示。有时它完美地工作,并且检查包含应该检查的标题的单元格,并且不检查不应该检查的单元格。但是,当一个单元格收到支票时,表格视图会向另一个不应检查的单元格添加随机复选标记。但这只发生在某些列表中,即使它可能包含与另一个工作正常的列表完全相同的数据。单元格包含在最多可能有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);
}
}
}
}
}
}
答案 0 :(得分:0)
细胞被重复使用。当您有条件地设置单元格的属性时,必须始终为所有其他条件重置该属性。
-(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);
}
}
}
}
}
}