forkingdog / UITableView-FDTemplateLayoutCell

Template auto layout cell for automatically UITableViewCell height calculating
MIT License
9.94k stars 2.01k forks source link

各位注意了deleteSections一次删除多个会导致缓存错乱 #416

Closed cddjr closed 2 months ago

cddjr commented 3 years ago

因为IndexSet是按从小到大分布,FD枚举删除的时候没考虑到这个问题(其实和 Issue #54 是同一个bug) 解决方案是不应该用枚举的方式逐个删除,而应使用 removeObjectsAtIndexes直接一步删除

由于FD已经不再维护,所以我们要么整个项目clone下来改,要么只能使用Swizzle替换方法。

我选的后者,这里给出部分swift代码

@objc private func _deleteSections(_ sections: IndexSet, with animation: UITableView.RowAnimation) {
        guard fd_indexPathHeightCache.automaticallyInvalidateEnabled, !sections.isEmpty else {
            _deleteSections(sections, with: animation)
            return
        }

        // FD框架有严重bug,我们自己来
        let portrait = fd_indexPathHeightCache.value(forKey: "_heightsBySectionForPortrait") as? NSMutableArray
        let landscape = fd_indexPathHeightCache.value(forKey: "_heightsBySectionForLandscape") as? NSMutableArray

        portrait?.removeObjects(at: sections)
        landscape?.removeObjects(at: sections)

        // 我们已经处理好了,避免FD的bug代码二次处理
        fd_indexPathHeightCache.automaticallyInvalidateEnabled = false
        defer {
            fd_indexPathHeightCache.automaticallyInvalidateEnabled = true
        }
        _deleteSections(sections, with: animation)
    }