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 クラスからmy を初期化します。

- (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 部分で呼び出したいと思っています。

私が何か間違ったことをしたかどうか誰かが見ることができますか? ありがとう ;-)

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 に答える