UITableView 删除带有动画的单元格? iOS (Objective C)

UITableView deletion of cell with animation? iOS (Objective C)

我搜索了一下,似乎大多数建议都是简单地 [tableView reloadData]。

但是,我正在处理删除单元格的动画。如果您可以想象一个清单,我将项目添加到清单中,一旦清单上的 item/task(例如:"Get milk from milk bar")完成,我通过点击它删除 item/task。此时我希望单元格随着动画消失。

正如上面提到的,大多数人只是推荐一条路线,这涉及我从它所在的数组中删除项目并简单地重新加载 tableview,但这完全没有动画。

以下是我尝试过的替代方案,但收效甚微。

[tableView beginUpdates]
[tableView deleteRowsAtIndexPaths:[self.taskArray objectAtIndex:indexPath.row] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates]

注意:taskArray 填充了我输入的任务。此外,上述代码位于 "didSelectRowAtIndexPath" 方法中。 关于我做错了什么或我可以做什么的提示?

TLDR;我想通过点击它来从 tableView 中删除带有动画的单元格。

快速观察一下,API 要求您提供一组索引路径,并且您只传递要删除的实际项目。

如果你尝试会发生什么:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

在你的didSelectRowAtIndexPath

// Delete from the taskArray
[self.taskArray removeObjectAtIndex:indexPath.row];

// Remove row from the tableview
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];