Uitableview 在IOS 7中,dequeueReusableCellWithIdentifier返回处于编辑状态的单元格

Uitableview 在IOS 7中,dequeueReusableCellWithIdentifier返回处于编辑状态的单元格,uitableview,Uitableview,以下问题在7之前的IOS版本中不会出现 使用UITableView的幻灯片编辑删除项目的界面,删除项目并滚动后,新显示的单元格(从dequeueReusableCellWithIdentifier返回)如下所示: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexP

以下问题在7之前的IOS版本中不会出现

使用UITableView的幻灯片编辑删除项目的界面,删除项目并滚动后,新显示的单元格(从
dequeueReusableCellWithIdentifier
返回)如下所示:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Find the item that this cell represents
        NSDictionary *item = [self itemForRowAtIndexPath:indexPath];
        if (!item) return;

        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [tableView endUpdates];

        // Remove it from the data store
        [Inventory removeInventoryItem:item];
    }
}

[该图像可能不会永远可用,因此这里有一个描述:滚动后,新单元格仍处于最终编辑状态,删除按钮仍可见,单元格内容在屏幕左侧。]

此外,返回的单元格甚至设置了其
编辑
标志

我用来删除单元格的代码如下所示:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Find the item that this cell represents
        NSDictionary *item = [self itemForRowAtIndexPath:indexPath];
        if (!item) return;

        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [tableView endUpdates];

        // Remove it from the data store
        [Inventory removeInventoryItem:item];
    }
}
我用黑客破解的方法解决了这个问题。代码(带有hack)为:

通过hack(while循环),将生成一个不处于编辑状态的新单元。这会导致一些内存浪费,但确实会产生正确的结果

我想知道在
prepareforeuse
方法中是否需要执行某些操作来重置编辑状态。目前,我的
prepareforeuse
方法仅使用默认值初始化内部控件(标签等)


我已尝试在删除单元格时,在
prepareforeuse
中以及每当
dequeueReusableCellWithIdentifier:
返回仍设置了
编辑
标志的单元格时,对UITableViewCell和UITableView两者调用
setEditing:animated:
,但似乎没有什么能解决这个问题。

我有这个问题,对我来说答案是“doh”时刻。确保在您自己的实现中调用
super
实现
prepareforeuse

谢谢Leo,就这样!过去两天我一直在研究这个问题(还有其他一些问题),所以我没有想过要这么做,我遇到的任何一个实现实际上都没有调用super的preprepreforreuse(我只是重复检查)。作为忏悔,我会在这些问题和博客上发布一条说明,说明他们应该这样做。干杯!它在文档中说得很清楚,但实际上很少被调用。之前:“为什么要阅读文档?我知道它的作用……调用它是为了让您在重用之前将单元格重置为默认值。”之后:“嘿,文档,我额头上有一个平点……”您的意思是我应该调用prepareforeuse吗?在您自己的
prepareforeuse
实现中,您应该确保调用
[super prepareforeuse]