奇妙な問題に遭遇しました。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 部分で呼び出したいと思っています。
私が何か間違ったことをしたかどうか誰かが見ることができますか? ありがとう ;-)