我试图使用DiffableDataSource集合视图,我一直收到这个错误
Fatal: supplied item identifiers are not unique. Duplicate identifiers下面是我实现它的方法
extension UICollectionView {
enum Section {
case main
}
}
class FeedsCollectionView: View {
var collectionView: UICollectionView! = nil
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
typealias DataSource = UICollectionViewDiffableDataSource<UICollectionView.Section, Scraper>
typealias DataSourceSnapshot = NSDiffableDataSourceSnapshot<UICollectionView.Section, Scraper>
private var dataSource : DataSource!
private var snapshot: DataSourceSnapshot!
public lazy var data: [Scraper] = [] {
didSet {
applySnapshot()
}
}
private func configureDataSource() {
dataSource = DataSource(collectionView: collectionView, cellProvider: { (collectionView, indexPath, data) in
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feeds", for: indexPath) as? FeedsCollectionViewCell {
cell.configure(with: data)
cell.eventHandler = { [weak self] events in
switch events {
case .reload:
self?.applySnapshot()
}
}
return cell
}
return UICollectionViewCell()
})
applySnapshot()
}
public func applySnapshot() {
snapshot = DataSourceSnapshot()
snapshot.appendSections([UICollectionView.Section.main])
snapshot.appendItems(data)
dataSource.apply(snapshot, animatingDifferences: true)
}
private func createLayout() -> UICollectionViewLayout {
return UICollectionViewCompositionalLayout { section, layoutEnvironment in
var config = UICollectionLayoutListConfiguration(appearance: .sidebar)
config.headerMode = section == 0 ? .none : .firstItemInSection
return NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment)
}
}
private func configureHierarchy() {
collectionView = UICollectionView(frame: bounds, collectionViewLayout: createLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(FeedsCollectionViewCell.self, forCellWithReuseIdentifier: "feeds")
addSubview(collectionView)
}
override func setupUI() {
configureHierarchy()
configureDataSource()
}
override func setupLayout() {
LayoutConstraint.pin(to: .bounds, collectionView).activate()
}
}在这里,剪贴类(它的一部分):
public class Scraper: Hashable {
let id = UUID()
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
public static func == (lhs: Scraper, rhs: Scraper) -> Bool {
lhs.id == rhs.id
}刮刀类要大得多,我做不到struct.How,我可以修复这个错误,非常感谢
发布于 2022-10-13 22:39:19
好像你在一遍又一遍地加同样的东西..。
https://stackoverflow.com/questions/74062471
复制相似问题