0

我遇到了一個奇怪的問題。 我有一個自定義的composant,爲我處理UICollectionView的佈局。 該代碼是用Swift編寫的。問題轉發UIScrollViewDelegate調用

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 
{ 

} 

最後,我的問題是這樣的: 我scrollViewDidScroll永遠不會被調用,而viewForZoomingInScrollView是。

如果我在MyCustomCollectionViewHandler上執行scrollViewDidScroll,則調用它。

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

有人可以看看我是否做錯了什麼?謝謝;-)

回答

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