Closed JhonnyTawk closed 5 years ago
Hello Jhonny,
Thank you for raising this issue.
So the MSPeekCollectionViewDelegateImplementation
is actually an implementation for the UICollectionViewDelegate
. I'm understanding you want to use the implementation but also still implement the delegate methods found in the UICollectionViewDelegate
is that correct?
If this is the case, you can actually do that by subclassing the MSPeekCollectionViewDelegateImplementation
and adding your own overridden methods. Please check Subclassing section for more information.
I am getting this issue
I am only inheriting UICollectionViewDataSource and now MSPeekCollectionViewDelegateImplementation
So this wouldn't work because MSPeekCollectionViewDelegateImplementation
is a concrete class not a protocol. Extensions only work when you extend a concrete class with a protocol. Instead you can do this:
1- Create a protocol which your RegisterPlansVC
will conform to:
protocol SelectionDelegate: AnyObject {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
}
2- Subclass the MSPeekCollectionViewDelegateImplementation
so we can add the selection delegate and map the delegate function:
class SelectablePeekCollectionViewDelegateImplementation: MSPeekCollectionViewDelegateImplementation {
weak var selectionDelegate: SelectionDelegate?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectionDelegate?.collectionView(collectionView, didSelectItemAt: indexPath)
}
}
3- in your RegisterPlansVC
, instantiate a variable of type SelectablePeekCollectionViewDelegateImplementation
and set the delegate in viewDidLoad
and extend the view controller to conform to the protocol we created in step 1 like this:
class RegisterPlansVC: UIViewController {
var peekImplementation: SelectablePeekCollectionViewDelegateImplementation!
//Your variables
override func viewDidLoad() {
super.viewDidLoad()
//Other initialization code
peekImplementation = SelectablePeekCollectionViewDelegateImplementation()
peekImplementation.selectionDelegate = self
}
}
extension RegisterPlansVC: SelectionDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Selected item at \(indexPath)")
}
}
After thinking about your question I just realized that implementing this kind of behavior is a bit complicated and I should come up with an easier solution to do it! Anyways let me know if something is unclear
Thank you!
It worked fine after that implementation.
The main issue was I want to implement willDisplayCell but when I inherited UICollectionViewDelegate I must initialize collectionView.delegate = self
so MSPeekCollectionViewDelegateImplementation won't work
Yes you're right it wouldn't work this way. I'm thinking of a way to make it easier to do that. Will update the repo if I find anything 👍
@JhonnyTawk I will close this issue, please feel free to open another issue if you find something else 👍
I am not able to use UICollectionViewDelegate when I am using MSPeekImplementationDelegate