0

我遇到了一个奇怪的问题。我有一个自定义合成器,可以为我处理 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
    }
}

MyCustomCollectionViewHandler从一个 Objective-C 类初始化我的:

- (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
{

}

最后,我的问题是: MyscrollViewDidScroll从未被调用,而viewForZoomingInScrollViewis。

如果我scrollViewDidScroll在 上实现MyCustomCollectionViewHandler,它会被调用。

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

有人可以看看我是否做错了什么?谢谢 ;-)

4

1 回答 1

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)
}
于 2016-06-07T09:20:51.440 回答