Ios UITableView不会取消选中第一个选定行

Ios UITableView不会取消选中第一个选定行,ios,uitableview,Ios,Uitableview,我有一个UITableView,用户可以在其中选择“检查多行” 我的控制器中有一个NSMutableArray来保存所选的项目,在我的cellForRowAtIndexPath方法中,我检查项目是否在该数组中,并相应地返回处于选中/未选中状态的单元格 代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NS

我有一个UITableView,用户可以在其中选择“检查多行”

我的控制器中有一个NSMutableArray来保存所选的项目,在我的cellForRowAtIndexPath方法中,我检查项目是否在该数组中,并相应地返回处于选中/未选中状态的单元格

代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = kContactCellReuseIdentifier;
    static NSString *searchIdentifier = kContactSearchCellReuseIdentifier;

    POContactCell *cell;

    // Configure the cell...
    if (tableView == self.tableView) {
        cell = (POContactCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.contact = self.contacts[indexPath.row];
        NSLog(@"Returned cell with name %@", cell.contact.name);
    } else {
        cell = (POContactCell*)[tableView dequeueReusableCellWithIdentifier:searchIdentifier forIndexPath:indexPath];
        cell.contact = self.searchResults[indexPath.row];
    }

    if ([self.selectedContacts containsObject:cell.contact])
    {
        NSLog(@"was checked");
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
        cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    POContactCell* tappedCell = (POContactCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"Selected contact %@", tappedCell.contact.name);
    if ([self.selectedContacts containsObject:tappedCell.contact]) {
        // cell is already selected, so deselect it
        NSLog(@"It's already selected, so deselect it");
        [self.selectedContacts removeObject:tappedCell.contact];
        tappedCell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        NSLog(@"It's not already selected, so select it");
        [self.selectedContacts addObject:tappedCell.contact];
        tappedCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];
}
这个代码可以工作。。。除了第一个选择。用户点击的第一个单元格将被选中,并且永远不会被选中。我从日志语句中看到,所有单元格都经历了完全相同的过程,并且它也正确地识别了第一个点击行的选择状态,即使附件视图没有反映它

在第一次选择之后,所有其他行都可以正常工作

有什么调试想法吗?

您应该将self.contacts[indexPath.row]或self.searchResults[indexPath.row]适当地放入所选项目的数组中,并在用户点击单元格时检查数组中是否存在这些对象。通过从数据源将cell.contact设置为对象并在数组中检查cell.contact,您几乎可以做到这一点。但我会尝试将对象直接放入数组中,例如

id contact = self.contacts[indexPath.row];
if ([self.selectedContacs containsObject:contact])
    ...
并停止检查cell.contact是否在数组中以确定选定的属性

在UITableView中,内存中有一小部分实际的UITableViewCell对象,它们可以重复使用。问题的根源很可能是这个,因为您正在检查cell.contact是否在所选项目集中;重新使用单元格时,除非您编写了自己的PrepareForuse,否则自定义属性的先前值可能不会被清除


这有意义吗?

我只是尝试只使用self.contacts数组。仍然存在相同的问题。您是否更改了DidSelectRowatineXpath:中的代码,以从相应的self.contacts或self.searchResults数组中提取联系人,然后检查该对象是否在您的所选项目数组中?是的,因此现在DidSelectRowatineXpath:具有self.contacts[indexPath.row]用于检查容器或添加/移除材料。但结果仍然是一样的,可能值得为对象添加一个isSelected BOOL属性,这样就不需要在单独的数组中跟踪它们,并且可以完全转储该数组。你觉得这个主意怎么样?我可以试试,但我不这么认为。我从my log语句中看到选择是正确的。但是第一个复选标记永远不会消失,即使我点击第一行两次,它也不会再出现在选择数组中。