问题转发UIScrollViewDelegate调用

时间:2016-06-07 08:54:31

标签: ios swift uicollectionview uiscrollviewdelegate uicollectionviewdelegate

我遇到了一个奇怪的问题。 我有一个自定义合成器,为我处理UICollectionView的布局。 该代码是用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) {
        //
    }
}

但是,我不想处理来自MyCustomCollectionViewHandler部分的所有其他委托回调(尤其是UIScrollViewDelegate部分)。 所以我想如果可能的话我会手动转发所有的委托调用,我用那个代码来做:

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
    }
}

我从Objective-C类初始化MyCustomCollectionViewHandler

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

我还在这里实现了UIScrollViewDelegate调用,如下所示:

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

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

}

最后,我的问题是: 我的scrollViewDidScroll从未被调用,而viewForZoomingInScrollView则是。

如果我在scrollViewDidScroll上实施MyCustomCollectionViewHandler,则会调用它。

但我希望在ObjC部分调用它。

有人能看出我做错了什么吗?谢谢; - )

1 个答案:

答案 0 :(得分:0)

我认为方法override func respondsToSelector(aSelector: Selector) -> Bool可能有问题。如果您不希望处理程序响应委托方法,您应该retrun false?像:

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