Ios 当我使用重载数据时,为什么IndExpathForSelectedRows会失败?如何修复它?

Ios 当我使用重载数据时,为什么IndExpathForSelectedRows会失败?如何修复它?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,在-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath方法中,我有以下代码部分: if ([tableView isEqual:cheeseTableView]) { cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCe

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
方法中,我有以下代码部分:

     if ([tableView isEqual:cheeseTableView]) {

    cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {



        cell.accessoryType = UITableViewCellAccessoryNone;




    } else {


        cell.accessoryType = UITableViewCellAccessoryCheckmark;



    }

    NSLog(@"Called didSelecRowAtIndexPath: cheeseTableView");

    self.cheeseIndexPath = [self.cheeseTableView indexPathsForSelectedRows];

}
该表具有以下功能:
cheeseTableView.allowsMultipleSelection=YES

在方法的末尾,我有下面一行:
[tableView reloadData]


当我按原样使用代码时,
[self.cheeseIndepath count]
总是返回一个。但是当我删除
reloadData
方法时,我得到了正确的计数,但没有显示应有的值。如何使其正确显示并获得正确的计数?

如果您试图根据选择打开/关闭复选标记,那么如果您使用单元重用,则使用单元本身不是一个好方法。您需要将开/关值存储在与数据行数相同的数组中,然后在didSelectRow中切换该值。在cellForRow中,使用此数组适当地设置复选标记。

这里有几个问题可以指导您正确的方向

1) 为什么需要重新加载tableView的数据?依我看,在你展示的代码中没有任何东西值得重新加载数据

2) self.cheeseIndexPath应该包含什么数组

如果希望保留已检查的所有单元格的indexPath,我将在选择代码中添加/删除indexPath右侧:

NSTableViewCell *currentCell = [tableView cellAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        // remove the indexPath from the array
        [self.cheeseIndexPath removeObject:indexPath];
        // change anything of the cell that was just selected
        currentCell.backgroundColor = [UIColor newColor];
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        // add the indexPath from the array
        [self.cheeseIndexPath addObject:indexPath];
        // change anything of the cell that was just selected
        currentCell.backgroundColor = [UIColor newColor];
    }
3)
[tableView indexPathsForSelectedRows]
不会返回tableView中带有
cell.accessoryType==UITableViewCellAccessoryCheckmark
的所有单元格

此外,值得一提的是,所有不可见的单元都应被视为nil(因为存在排队机制)

希望这有助于


编辑
在代码片段中,我添加了注释和用于处理单元格背景颜色变化的代码(以回答@SonOfSeuss注释,而不是OP)。

试试这段代码。在我的例子中,它在didSelectRowAtIndexPath方法中起作用

NSMutableArray * checkedCells = [NSMutableArray alloc] init];

    for (UIView *view in tableView.subviews) {
            for (ProductAddonTableViewCell *cell in view.subviews) {
                if (cello.accessoryType == UITableViewCellAccessoryCheckmark)                {      
                    [checkedCells addObject:cell];
                }
            }
    }

您的解决方案实现不起作用。选择单元格后,
重新加载
使黑色背景变为白色。此外,在您的解决方案中,无论我是否退出
重新加载
,都不会保存任何内容。如果我退出
重新加载
,一切都会正常工作。就像我说的,你给我们看的代码中没有任何东西可以证明重新加载数据是正确的,所以我会删除它。但它可能有我们看不到的作用。对于代码的其余部分,我直接写在了答案框中,所以请原谅我可能犯的任何错误。不过,我多次使用该逻辑,因此该逻辑应该可以工作。哦,非常重要的是:我假设您将self.cheeseindepath从NSArray更改为NSMutableArray。在属性声明和初始化属性声明时应用该更改。去掉
重新加载
只是隐藏了基本的设计缺陷。您不应该更改didSelectRow中的单元格,而应该在数据源中标识该单元格。然后,当调用reloadData时,cellForRow应该检查数据源并正确修改单元格。一般规则:除非在cellForRowAtIndexPath中,否则不要修改单元格。