From 8de5fd3c349cdd8d47182da19be77c861bc2873d Mon Sep 17 00:00:00 2001 From: doge1024 <867129306@qq.com> Date: Sun, 2 Apr 2023 17:38:53 +0800 Subject: [PATCH] Fixed: View would blink and scrolling when the list changed When the contents of the list change, the list view scrolls from its initial position to the current page, and the contents blink --- .../PagingCollectionViewController.swift | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/Lib/SwiftUI/PagingCollectionViewController.swift b/Lib/SwiftUI/PagingCollectionViewController.swift index 3969f1e..df15aa0 100644 --- a/Lib/SwiftUI/PagingCollectionViewController.swift +++ b/Lib/SwiftUI/PagingCollectionViewController.swift @@ -56,16 +56,28 @@ public class PagingCollectionViewController 0 { + collectionView?.reloadItems(at: needsUpdateList.map({ IndexPath(item: $0, section: 0) })) + } else { + collectionView?.reloadData() + } layout.invalidateLayoutInBatchUpdate(invalidateOffset: true) } let index = currentIndex ?? layout.currentPage @@ -149,12 +161,18 @@ public class PagingCollectionViewController(as list: [T]) -> Bool + func listDiff(as list: [T]) -> [Int]? } extension PagingCollectionViewController: PagingCollectionViewControllerEquatableList where ValueType: Equatable { func isListSame(as list: [T]) -> Bool { self.list == (list as? [ValueType]) } + + func listDiff(as list: [T]) -> [Int]? { + guard let list = list as? [ValueType] else { return nil } + return self.list.listDiffIndex(otherList: list) + } } private extension UICollectionView { @@ -173,3 +191,18 @@ private extension UICollectionViewCell { String(describing: self) } } + +private extension Array where Element: Equatable { + func listDiffIndex(otherList: [T]) -> [Int]? { + if self.count != otherList.count { + return nil + } + var diff: [Int] = [] + for index in 0..