IOS separate scrollView & collectionView delegates to separate files

I have a UICollectionView and want to be able to execute custom behavior when the user scrolls through the implementation of scrollView delegation methods. Is it possible to have two separate objects that act as a collectionView delegate and a scrollView delegate when working with CollectionView?

+4
source share
3 answers

You cannot have individual delegates. UICollectionViewis a subclass UIScrollViewand overrides its property delegateto change its type to UICollectionViewDelegate(which is a subtype UIScrollViewDelegate). That way, you can only assign one delegate to represent the collection, and it can implement any combination of UICollectionViewDelegateand methods UIScrollViewDelegate.

However, you can redirect methods UIScrollViewDelegateto another object without much difficulty. Here's how you do it in Swift; it will be very similar to Objective-C (since all this is done using the Objective-C runtime):

import UIKit
import ObjectiveC

class ViewController: UICollectionViewController {

    let scrollViewDelegate = MyScrollViewDelegate()

    override func respondsToSelector(aSelector: Selector) -> Bool {
        if protocol_getMethodDescription(UIScrollViewDelegate.self, aSelector, false, true).types != nil || protocol_getMethodDescription(UIScrollViewDelegate.self, aSelector, true, true).types != nil {
            return scrollViewDelegate.respondsToSelector(aSelector)
        } else {
            return super.respondsToSelector(aSelector)
        }
    }

    override func forwardingTargetForSelector(aSelector: Selector) -> AnyObject? {
        if protocol_getMethodDescription(UIScrollViewDelegate.self, aSelector, false, true).types != nil || protocol_getMethodDescription(UIScrollViewDelegate.self, aSelector, true, true).types != nil {
            return scrollViewDelegate
        } else {
            return nil
        }
    }

Note that MyScrollViewDelegateit should probably be a subclass NSObjectfor this.

+6
source

, UICollectionViewController UICollectionViewDelegate. scrollView, View

0

Create a subclass of UICollectionViewController and write scroll scroll delegates in it.

class CustomCollectionViewController: UICollectionViewController {

    override func scrollViewDidScroll(_ scrollView: UIScrollView) {

    }
}

In your target class

class MyCollectionViewController: CustomCollectionViewController, UICollectionViewDelegateFlowLayout {
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
      return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return 100
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

      return cell
    }
}
0
source

Source: https://habr.com/ru/post/1621313/


All Articles