MaherKSantina / MSPeekCollectionViewDelegateImplementation

A custom paging behavior that peeks the previous and next items in a collection view
https://medium.com/@maher.santina90/how-to-create-cells-that-peek-on-the-sides-like-ios-11-app-store-ef4bb54c0c7d
MIT License
356 stars 32 forks source link

Can I use UICollectionViewDelegate & MSPeekImplementationDelegate together? #30

Closed JhonnyTawk closed 5 years ago

JhonnyTawk commented 5 years ago

I am not able to use UICollectionViewDelegate when I am using MSPeekImplementationDelegate

MaherKSantina commented 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.

JhonnyTawk commented 5 years ago

I am getting this issue

screen shot 2019-01-10 at 1 12 15 pm
JhonnyTawk commented 5 years ago

I am only inheriting UICollectionViewDataSource and now MSPeekCollectionViewDelegateImplementation

MaherKSantina commented 5 years ago

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

JhonnyTawk commented 5 years ago

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

MaherKSantina commented 5 years ago

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 👍

MaherKSantina commented 5 years ago

@JhonnyTawk I will close this issue, please feel free to open another issue if you find something else 👍