0

I ran into a weird issue. I have a custom composant that handles the layout of a UICollectionView for me. That code is written in Swift.

class MyCustomCollectionViewHandler: NSObject {

    let collectionView: UICollectionView
    weak var dataSource: UICollectionViewDataSource?
    weak var delegate: UICollectionViewDelegate?

    init(collectionView: UICollectionView) {
        self.collectionView = collectionView
        super.init()
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }

    // Rest of the code not relevant here
}

extension MyCustomCollectionViewHandler: UICollectionViewDataSource, UICollectionViewDelegate {

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //
    }
}

However, i do not want to handle all the others delegate callbacks from the MyCustomCollectionViewHandler part (especially the UIScrollViewDelegate ones). So i figured that i would forward all the delegate calls by hand if possible, i do that with that code :

extension MyCustomCollectionViewHandler {

    // forwarding selector if we don't implement it
    override func forwardingTargetForSelector(aSelector: Selector) -> AnyObject? {
        if delegate?.respondsToSelector(aSelector) ?? false {
            return delegate
        }
        if dataSource?.respondsToSelector(aSelector) ?? false {
            return dataSource
        }
        return super.forwardingTargetForSelector(aSelector)
    }

    override func respondsToSelector(aSelector: Selector) -> Bool {
        return super.respondsToSelector(aSelector) || delegate?.respondsToSelector(aSelector) ?? false || dataSource?.respondsToSelector(aSelector) ?? false
    }
}

And i init my MyCustomCollectionViewHandler from an Objective-C class :

- (void)initCollectionStructure
{
    self.collectionViewHandler = [[MyCustomCollectionViewHandler alloc] initWithCollectionView:self.collectionView];
    //-- So all the other calls fallback here
    self.collectionViewHandler.delegate = self;
}

And i also implement UIScrollViewDelegate calls here, as follows :

- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}

At last, my issue is this : My scrollViewDidScroll is never called whereas the viewForZoomingInScrollView is.

If i implement scrollViewDidScroll on the MyCustomCollectionViewHandler, it is called.

But i want it to be called on the ObjC part.

Can someone see if i did something wrong ? Thanks ;-)

Rom.
  • 2,800
  • 2
  • 17
  • 19

1 Answers1

0

I think the method override func respondsToSelector(aSelector: Selector) -> Bool maybe with something wrong with it. If you don't want your handler respond the delegate method, you should retrun false? like:

override func respondsToSelector(aSelector: Selector) -> Bool {
    if delegate?.respondsToSelector(aSelector) ?? false {
        return false
    }
    if datasource?.respondsToSelector(aSelector) ?? false {
        return false
    }
    return super.respondsToSelector(aSelector)
}
Klein Mioke
  • 1,261
  • 2
  • 14
  • 23