首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >致命:提供的项目标识符不是唯一的。DiffableDataSource中的重复标识符

致命:提供的项目标识符不是唯一的。DiffableDataSource中的重复标识符
EN

Stack Overflow用户
提问于 2022-10-13 22:30:21
回答 1查看 38关注 0票数 0

我试图使用DiffableDataSource集合视图,我一直收到这个错误

代码语言:javascript
复制
Fatal: supplied item identifiers are not unique. Duplicate identifiers

下面是我实现它的方法

代码语言:javascript
复制
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()
    }
    
}

在这里,剪贴类(它的一部分):

代码语言:javascript
复制
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,我可以修复这个错误,非常感谢

EN

回答 1

Stack Overflow用户

发布于 2022-10-13 22:39:19

好像你在一遍又一遍地加同样的东西..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74062471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档