首页 文章

删除动画完成后更新TableView [重复]

提问于
浏览
4

这个问题在这里已有答案:

我有一个tableview填充了一组对象的数据 . 物品有价格,名称等等 .

删除单元格时,将更新数据源(数组),并使用UITableViewRowAnimationFade将该行滑出屏幕 .

删除项目时,阵列中对象的某些属性(如价格)可能会发生变化,因此我需要更新屏幕上的所有单元格,因为它们的数据可能已更改 .

我查看了docs并找到了reloadRowsAtIndexPaths:withRowAnimation,我可以将其与indexPathsforVisibleRows结合起来重新加载屏幕上的行但是在tableView中执行此操作:commitEditingStyle:forRowAtIndexPath看起来非常讨厌,因为它尝试执行删除动画同时还重新加载...

有没有办法在执行任务之前等待删除动画完成?

这是我的ViewController的代码

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        // update the datasource
        [self.dataController deleteItemAtIndex:indexPath.row];

        // update the table
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        // reload the table to show any changes to datasource from above deletion
        [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
    }
}

ASD

1 回答

  • 1

    编辑:

    好的下一次尝试 . :)这应该肯定有用,但它需要相当多的努力......

    您需要准确跟踪数据源中的更改(添加,删除,更新)并在TableVC上调用相应的方法 .

    数据源源需要提供以下委托方法:

    - (void)dataControllerWillUpdateData;
    - (void)dataControllerDidRemoveObjectAtIndexPath:(NSIndexPath *)indexPath;
    - (void)dataControllerDidAddObjectAtIndexPath:(NSIndexPath *)indexPath;
    - (void)dataControllerDidUpdateObjectAtIndexPath:(NSIndexPath *)indexPath;
    - (void)dataControllerDidUpdateData;
    

    然后你改变tableVC的实现,如下所示:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) 
        {
        // update the datasource
        [self.dataController deleteItemAtIndex:indexPath.row];
        }
    }
    
    
    - (void)dataControllerWillUpdateData
    {
        [tableView beginUpdates];
    }
    
    - (void)dataControllerDidRemoveObjectAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    - (void)dataControllerDidAddObjectAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    - (void)dataControllerDidUpdateObjectAtIndexPath:(NSIndexPath *)indexPath
    {
        [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
    }
    
    - (void)dataControllerDidUpdateData
    {
        [tableView endUpdates];
    }
    

    因此,如果用户删除了一个单元格,您的数据源必须确定哪些其他对象受到影响,创建一个更改列表(小心计算正确的indexPaths),调用 dataControllerWillUpdateData ,为每个更改的对象调用上面的相应方法,最后调用 dataControllerDidUpdateData .

    当然,您也可以考虑在项目中使用CoreData . 这可能需要一些工作来设置一切,但结果你会获得上面提到的所有内容以及更多“免费” . 就个人而言,我倾向于将它用于几乎每个包含动态tableViews的项目 . 它有很多好处,大部分时间都值得付出努力 .

相关问题