0

So, I have implemented a protocol, called ResuableCell

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

Now, I wish to conform UICollectionViewCell to it, so that all my subsequent UICollectionViewCell will be formed to implement such a method.

Anyone knows how to achieve this?

Thanks

3
  • extension UICollectionViewCell: ResuableCell {} Commented Jun 11, 2018 at 8:29
  • @Scriptable that would mean that all the UICollectionViewCell shares the same function, which produces the same identifier string. What I want is an individual function to produce individual identifier string Commented Jun 11, 2018 at 8:31
  • that wouldn't do that because it doesnt define a function or any functionality. it just says that all UICollectionViewCell's must confirm to that protocol. you could also provide default functionality and return the string version of the class name as the identifier.. making it automatically filled and unique Commented Jun 11, 2018 at 8:32

2 Answers 2

2

You can use reusable protocol and protocol extensions to return the reuseIdentifier value. Then add extension on UICollectionViewCell and confirm the reusableCell protocol so that every UICollectionviewCell subclass will have the reusableIdentifier available.

protocol ReusableCell: AnyObject { static var reuseIdentifier: String { get } } extension ReusableCell { static var reuseIdentifier: String { return String(describing: self) } } extension UICollectionViewCell: ResuableCell {} 

There is reusable library available which does this nicely. You can follow this blog on how to implement this feature.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that is what I need
0

extend your custom uiCollectionViewCell with ReusableCell

extension CustomCollectionViewCell : ReusableCell { } 

don't forget to assign the delegate

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.