iOS 中的列表视图:UICollectionView 和 UITableView 可以在兼顾性能的同时让开发者比较容易地搭建复杂的 App UI 界面,并且用户的交互行为也变得顺畅。如果要百尺竿头更进一步的话,我们需要按需刷新列表而不是一股脑儿地调用 reloadData。在 iOS 13 之前,我们可以通过 IndexPath 或者 IndexSection 来进行局部刷新,但却几乎总是会遇到这个崩溃:
Terminating app due to uncaught exception’NSInternalInconsistencyException’,reason: ‘Invalid update: invalid number of sections. The number of sections contained in the tableView view after the update (1) must be equal to the number of sections contained in the tableView view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).
而造成崩溃本质上的原因是模型层和渲染层的数据不一致。考虑到了开发者的“民间疾苦“,在 iOS 13 之后,Apple 提供了一套新的 API 用于 Diff 数据源,还附送优雅的刷新动画,You deserve it!