在具有自定义布局的 CollectionView 中未调用 scrollViewDidScroll

scrollViewDidScroll not called in a CollectionView with custom layout

我想在用户到达我的 CollectionView 末尾时加载更多数据。不幸的是,永远不会调用 scrollViewDidScroll。

对于我的 CollectionView,我使用了自定义布局,我认为问题可能在于此:

    if let layout = exploreCollectionView.collectionViewLayout as? CustomLayout {
        layout.delegate = self
    }

我的class:

class MainViewController: UIViewController, UIScrollViewDelegate { .....

我想检查函数 scrollViewDidScroll 是否有效:

    // Check if a user wants to load more data at the bottom
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print("scrolled")
}

如何在我的 CustomLayout 中实现 scrollViewDidScroll?

scrollViewDidScroll 是一种 UIScrollViewDelegate 方法。所以你需要让自己(MyViewController)成为collectionview的委托:

exploreCollectionView.delegate = self

然后例如在扩展中:

extension MyViewController: UIScrollViewDelegate {
   func scrollViewDidScroll(_ scrollView: UIScrollView) {
      print("scrolled")
   }
}