UITableView:删除带有动画的部分

2024-01-07

Update

我已将我对此问题的解决方案作为答案发布在下面。它采用了与我的第一次修订不同的方法。


原始问题我之前问过一个问题,我认为解决了我的问题:

删除行时如何处理不可见行。 (UITableViews) https://stackoverflow.com/questions/998603/how-to-deal-with-non-visible-rows-during-row-deletion-uitableviews

但是,当我从 UITableView 中删除部分时,我现在再次遇到类似的问题。 (当我改变表中的部分/行数时,它们重新出现)。

在我因为帖子的长度而失去您之前,让我清楚地说明问题,您可以根据需要阅读以提供答案。


Problem:

如果从 UITableView 批量删除行和部分,应用程序有时会崩溃。这取决于表的配置以及我选择删除的行和部分的组合。

日志说我崩溃了,因为它说我没有正确更新数据源和表:

Invalid update: invalid number of rows in section 5.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).

现在,在您写出明显的答案之前,我向您保证我确实已从数据源中正确添加和删除了行和部分。解释很长,但是您可以按照下面的方法找到它。

因此,如果您仍然感兴趣……


处理删除部分和行的方法:

- (void)createFilteredTableGroups{

    //index set to hold sections to remove for deletion animation
    NSMutableIndexSet *sectionsToDelete = [NSMutableIndexSet indexSet];
    [sectionsToDelete removeIndex:0];


    //array to track cells for deletion animation
    NSMutableArray *cellsToDelete = [NSMutableArray array];

    //array to track controllers to delete from presentation model
    NSMutableArray *controllersToDelete = [NSMutableArray array];

    //for each section
    for(NSUInteger i=0; i<[tableGroups count];i++){

        NSMutableArray *section = [tableGroups objectAtIndex:i];

        //controllers to remove
        NSMutableIndexSet *controllersToDeleteInCurrentSection = [NSMutableIndexSet indexSet];
        [controllersToDeleteInCurrentSection removeIndex:0];
        NSUInteger indexOfController = 0;

        //for each cell controller
        for(ScheduleCellController *cellController in section){

            //bool indicating whether the cell controller's cell should be removed
            NSString *shouldDisplayString = (NSString*)[[cellController model] objectForKey:@"filteredDataSet"];
            BOOL shouldDisplay = [shouldDisplayString boolValue];

            //if it should be removed
            if(!shouldDisplay){

                NSIndexPath *cellPath = [self indexPathOfCellWithCellController:cellController]; 

                //if cell is on screen, mark for animated deletion
                if(cellPath!=nil)
                    [cellsToDelete addObject:cellPath];

                //marking controller for deleting from presentation model
                [controllersToDeleteInCurrentSection addIndex:indexOfController];                

            }
            indexOfController++;
        }

        //if removing all items in section, add section to removed in animation
        if([controllersToDeleteInCurrentSection count]==[section count])
            [sectionsToDelete addIndex:i];

        [controllersToDelete addObject:controllersToDeleteInCurrentSection];

    }


    //copy the unfiltered data so we can remove the data that we want to filter out
    NSMutableArray *newHeaders = [tableHeaders mutableCopy];
    NSMutableArray *newTableGroups = [[allTableGroups mutableCopy] autorelease];


    //removing controllers
    int i = 0;
    for(NSMutableArray *section in newTableGroups){
        NSIndexSet *indexesToDelete = [controllersToDelete objectAtIndex:i];
        [section removeObjectsAtIndexes:indexesToDelete];
        i++;
    }

    //removing empty sections and cooresponding headers
    [newHeaders removeObjectsAtIndexes:sectionsToDelete];
    [newTableGroups removeObjectsAtIndexes:sectionsToDelete];

    //update headers
    [tableHeaders release];
    tableHeaders = newHeaders;

    //storing filtered table groups
    self.filteredTableGroups = newTableGroups;


    //filtering animation and presentation model update
    [self.tableView beginUpdates];
    tableGroups = self.filteredTableGroups;
    [self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView endUpdates];


    //marking table as filtered
    self.tableIsFiltered = YES; 


}

我猜:

问题似乎是这样的:如果您查看上面我列出的每个部分中的单元格数量,您会发现第 5 部分似乎增加了 1。然而,事实并非如此。原来的第 5 节实际上已被删除,并由另一个节取代(具体来说,它是旧的第 10 节)。

那么为什么表视图似乎没有意识到这一点呢?它应该知道我删除了旧的部分and不应期望现在位于旧节索引处的新节受到已删除节的行数的限制。

希望这是有道理的,写出来有点复杂。

(请注意,此代码之前适用于不同数量的行/部分。这个特定的配置似乎给它带来了问题)


我以前遇到过这个问题。您正尝试删除某个部分中的所有行,然后删除现在为空的部分。但是,仅删除该部分就足够(并且正确)了。其中的所有行也将被删除。这是我的项目中处理一行删除的一些示例代码。它需要确定是否应该从节中仅删除这一行,或者如果它是该节中最后剩余的行,则删除整个节:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // modelForSection is a custom model object that holds items for this section.
        [modelForSection removeItem:[self itemForRowAtIndexPath:indexPath]];

        [tableView beginUpdates];

        // Either delete some rows within a section (leaving at least one) or the entire section.
        if ([modelForSection.items count] > 0)
        {
            // Section is not yet empty, so delete only the current row.
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        }
        else
        {
            // Section is now completely empty, so delete the entire section.
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
                     withRowAnimation:UITableViewRowAnimationFade];
        }

        [tableView endUpdates];
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UITableView:删除带有动画的部分 的相关文章

  • UIToolBar 位于 UITabBar 之上?

    在 照片 应用程序的 相机胶卷 部分中 您可以单击右上角的按钮来编辑照片 这将底部选项卡栏替换为 UIToolBar 中的三个新按钮 我想做类似的事情 但我无法让我的 UIToolBar 位于选项卡栏的顶部 有没有办法指定图层顺序 或者我需
  • 如何在 iPhone 中使用彩信发送音频?

    我瞪大了眼睛MMS在 iPhone 中 但我没有找到太多这方面的信息 大多数发现都与图像有关 我想使用发送音频MMS in iPhone using ios sdk 可以这样做吗 我有以下疑问MMS 如何识别所有MMS音频文件在iPhone
  • 如何使 UITableView 可重新排列?

    我正在尝试使我的 UITableView 可编辑 以便您可以移动单元格 现在 当我单击编辑按钮时 它只允许我删除 但不能重新排列 我有的方法有 Code BOOL tableView UITableView tableView canEdi
  • 如何构建 Objective-C 静态库? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有一些 Objective C 类 目前在 Cocoa 应用程序 Mac OS X 和 Cocoa Touch 应用程序 iOS 中
  • 隐藏故事板中的导航栏

    谁能告诉我如何隐藏故事板中的导航栏 我下面的代码在模拟器中运行时工作正常 但它仍然出现在我的故事板中 这真的很烦我 因为它弄乱了我的图像的位置 有人可以帮忙吗 void viewWillAppear BOOL animated super
  • 如何在 iPhone sdk 上访问年龄限制家长控制

    有没有办法以编程方式访问 iPhone iPad iPod 上家长控制中设置的年龄限制 如果年龄限制太低 我想禁止使用某些功能 我一直在四处寻找 但也许我没有使用正确的搜索词或其他东西 任何帮助表示赞赏 也许这样行不通 thanks 从 3
  • 获取在 iOS UIFont 中追踪字符的路径

    假设我在 iOS 应用程序中使用了自定义字体 Foo 我已将其添加到我的项目 plist 等中 并且我能够渲染UILabels之类的就很好了 现在 如果我想找出可以 追踪 该字体中的字母 P 的点序列 我将如何获得该点序列 例如 假设我想使
  • iOS 低内存崩溃,但内存使用率非常低

    这已经困扰我很长时间了 我的应用程序运行占用大约 2 74MB 内存 没关系 但当它创建 UIWebView 时 它会增加到大约 5 87MB 并继续崩溃 这些是在我的第一代 iPad 上运行时仪器中的实时字节下给出的值 我找不到崩溃日志
  • 自定义信息窗口上的按钮未接收 ios 中的操作

    我在 iOS 应用程序中使用 Google 地图 并实现了一个自定义信息窗口来显示标记的标题 现在 我在该自定义信息窗口上添加了一个按钮 但我的问题是按钮操作方法没有被调用 自定义信息窗口 h import
  • 从本机 iPhone 应用程序访问 UIWebView 本地存储数据

    我正在编写一个包含 UIWebView 组件的本机 iPhone 应用程序 该组件访问互联网 Web 应用程序 并且该 Web 应用程序使用 HTML5 本地存储离线存储数据 是否可以从本机应用程序访问此本地存储数据 我想做类似的事情 简而
  • iOS 上关键 ClientState 警告的默认访问速度缓慢

    在测试我的 iOS 应用程序时 我收到 对关键 ClientState 的默认访问速度慢 耗时 0 034635 秒 容差为 0 020000 警告 它似乎是间歇性发生的 我试图环顾四周看看它是关于什么的 但我并不完全确定 任何帮助表示赞赏
  • iPhone 的翻译器?

    我对为 iPhone 制作一个解释器很感兴趣 这将是一个实验性的想法 但可能会很棒 我喜欢让我自 己的语言适合移动计算和数学的想法 我查阅了很多资料 发现有关 iPhone 上的口译员的信息很复杂 苹果会允许什么 我见过这个应用程序 这是一
  • 使用 iOS 修剪音频

    我想实现一项功能 让用户修剪他之前录制的音频文件 caf 录音部分已经可以工作了 但是我如何添加类似于 Voicememos 应用程序中的修剪功能 苹果使用的音频修剪器有API吗 任何帮助都会很棒 使用 AVFoundation 怎么样 将
  • 日期时间到 NSDate

    如何转换字符串2010 11 19T20 00 00Z进入一个NSDate object 我尝试过使用 dateFormatter setDateFormat yyyy MM ddTHH mm ssZ 但看起来我的自定义格式样式错误 PS
  • 检测 iPhone 屏幕是否打开/关闭

    有没有办法检测 iPhone 的屏幕是打开还是关闭 例如 当按下手机的屏幕锁定按钮时 我一直在使用 void applicationWillResignActive UIApplication application 为此类事件做准备 在大
  • Grand Central Dispatch (GCD) 调度源标志

    我最近不再使用 to GCD 调度来源 https developer apple com documentation dispatch 1385630 dispatch source create监视文件更改 效果很好 API 也变得更加
  • 按升序对 NSDictionary 进行排序

    我正在尝试排序NSDictionary按升序排列 我正在使用这段代码 NSDictionary valDict self mGetDataDict key rowKey for NSString valueKey in valDict al
  • iPhone Dev:从子控制器和 TabBar 中的另一个控制器重新加载表视图的数据

    我正在实现一个笔记管理器应用程序 它在 tabBar 中有一个 tableView 其中显示其笔记的主要信息 访问一个注释时 您可以编辑其属性 在 tabBar 的一个按钮中 您可以选择注释在 tableView 中的显示方式 我遇到的问题
  • 如何将nsmutable数组添加到sqlite数据库表中

    如何将nsmutablearray添加到sqlite数据库表中 有人可以帮我编码吗 您可以使用 for int i 0 i lt mutArray count i NSString string mutArray objectAtIndex
  • 隐藏 UITableview 单元格

    我正在尝试从 UITableView 中隐藏单元格 就像删除操作一样 但我只想隐藏它以便稍后在相同位置显示它 我知道 UITableViewCell 有一个名为 隐藏 的属性 但是当我使用此属性隐藏单元格时 它会隐藏但没有动画 并且会留下空

随机推荐