2

我想为我的集合/表格视图调整一些滚动视图方法来跟踪一些事件。我收到错误,因为它无法在 collectionview 中找到 scrollViewDidScroll: Method。我尝试使用下面的代码

extension UICollectionView
{

    public override class func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }

        // make sure this isn't a subclass
        if self !== UICollectionView.self {
            return
        }

        dispatch_once(&Static.token) {
            let originalSelector = #selector(self.scrollViewDidScroll(_:))
            let swizzledSelector = #selector(self.sp_scrollViewDidScroll(_:))

            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

            let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

            if didAddMethod {
                class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod)
            }
        }
    }

    // MARK: - Method Swizzling

    func sp_scrollViewDidScroll(scrollView:UIScrollView) {
        self.sp_scrollViewDidScroll(animated)
        print("Swizzled sp_scrollViewDidScroll in place of scrollViewDidScroll")
    }
}

错误:类型 UICollectionView 没有成员“scrollViewDidScroll”

4

1 回答 1

1

我认为您在 UIScrollViewDelegate 协议中找到了该方法,而不是在类本身中。在你的 collectionView 的委托中实现它(UICollectionViewDelegate extends UIScrollViewDelegate)。最好的

于 2016-04-06T14:34:32.630 回答