使所有子类符合特定协议

时间:2018-06-11 08:25:22

标签: ios swift

所以,我已经实现了一个名为ResuableCell

的协议
protocol ResuableCell{
static func resuableIdentifier()->String}

现在,我希望将UICollectionViewCell与它一致,以便形成所有后续的UICollectionViewCell来实现这样的方法。

任何人都知道如何实现这一目标?

谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用可重用协议和协议扩展来返回reuseIdentifier值。然后在UICollectionViewCell上添加扩展并确认reusableCell协议,以便每个UICollectionviewCell子类都可以使用reusableIdentifier。

protocol ReusableCell: AnyObject {
    static var reuseIdentifier: String { get }
}

extension ReusableCell {
    static var reuseIdentifier: String {
        return String(describing: self)
    }
}

extension UICollectionViewCell: ResuableCell {}

reusable library可用,这很好。您可以按照blog了解如何实施此功能。

答案 1 :(得分:0)

使用ReusableCell扩展您的自定义uiCollectionViewCell

extension CustomCollectionViewCell : ReusableCell { 
}

不要忘记分配代表

相关问题