UICollectionViewDiffableDataSource 自定义子类初始化

问题描述

所以我正在制作 UICollectionViewDiffableDataSource 的自定义子类。

我的代码如下:

class MyDiffableDataSource: UICollectionViewDiffableDataSource<String,String> {
    
    convenience required init(collectionView: UICollectionView) {
        self.init(collectionView: collectionView) { (collection,index,item) in
            if index.item % 2 == 0 {
                return self.firstCell(collection,item,index)
            } else {
                return self.secondCell(collection,index)
            }
        }
        self.supplementaryViewProvider = { (_,_,index) in
            return self.header(collectionView,index)
        }
    }
    
    func firstCell(_ collection: UICollectionView,_ item: String,_ index: IndexPath) -> UICollectionViewListCell {
        let cellReg = UICollectionView.CellRegistration<UICollectionViewListCell,String> { (cell,item) in
            var content = cell.defaultContentConfiguration()
            content.text = item
            cell.contentConfiguration = content
            cell.accessories = [.disclosureIndicator()]
        }
        return collection.dequeueConfiguredReusableCell(using: cellReg,for: index,item: item)
    }
    
    func secondCell(_ collection: UICollectionView,item) in
            var content = cell.defaultContentConfiguration()
            content.text = item
            cell.contentConfiguration = content
            cell.accessories = [.checkmark()]
        }
        return collection.dequeueConfiguredReusableCell(using: cellReg,item: item)
    }
    
    func header(_ collection: UICollectionView,_ index: IndexPath) -> UICollectionReusableView {
        let headerReg = UICollectionView.SupplementaryRegistration
        <UICollectionReusableView>(elementKind: UICollectionView.elementKindSectionHeader) { (_,_) in }
        return collection.dequeueConfiguredReusableSupplementary(using: headerReg,for: index)
    }
}

但当然,我在 'self.init' 调用或赋值给 'self' 之前使用了 'self' 来自 Xcode 的错误

如何仅使用 UICollectionView 参数初始化 MyDiffableDataSource 而不会出现此错误

我知道我可以将函数 firstCell()secondCell() 设为静态,或者我可以将这两个函数的所有内容直接放在 init.d 中。但是,如果我稍后添加更多内容,那将不会很好,并且静态会阻止我访问其他非静态属性

还有其他我可以使用的@R_404_6280@案吗?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)