如何快速在 UICollectionView 中添加分页?

Tec*_*ain 8 xcode ios uicollectionview swift

我有一个显示项目的 UICollection 视图。现在我想在 UICollectionView 上添加分页。我不想为此功能使用任何第三方。请让我知道我如何实现这一目标!

我有四个一个例子http://slicode.com/bottom-refresh-control-uicollectionview/

但是在此我必须将滚动视图向上拖动几秒钟才能使其工作。

Ket*_*dra 7

做到这一点的简单方法

 var page: Int = 0
 var isPageRefreshing:Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    YourApi(page1: 0)
}

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if(self.mycollectionview.contentOffset.y >= (self.mycollectionview.contentSize.height - self.mycollectionview.bounds.size.height)) {
        if !isPageRefreshing {
            isPageRefreshing = true
            print(page)
            page = page + 1
            YourApi(page1: page)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的 api 方法中

func YourApi(page1: Int) {
    let mypage = String(page1)
    let request: String = String.init(format:"apiName/%@", mypage)
    print(request)
}
Run Code Online (Sandbox Code Playgroud)

就是这样。


Jay*_*eep 5

如果要添加底部刷新控件,则必须使用下面的辅助类。

https://github.com/vlasov/CCBottomRefreshControl/tree/master/Classes

下载 Objective-C 中的这 2 个文件。您必须在 Bridging 标头中导入此文件

#import "UIScrollView+BottomRefreshControl.h"
Run Code Online (Sandbox Code Playgroud)

像这样添加刷新控件。

let refreshControl = UIRefreshControl.init(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
refreshControl.triggerVerticalOffset = 50.0 
refreshControl.addTarget(self, action: #selector(paginateMore), for: .valueChanged) 
collectionView.bottomRefreshControl = refreshControl
Run Code Online (Sandbox Code Playgroud)

这个助手类为您提供了新的bottomRefreshControl属性,它可以帮助您在底部添加刷新控件。


chi*_*hah 5

我在我的一个应用程序中添加了加载更多功能,所以让我给您代码

    func collectionView(_ collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        referenceSizeForFooterInSection section: Int) -> CGSize {

        if arrData.count > 0 && isLastPageReached == false
        {
            return CGSize(width:(collectionView.frame.size.width), height: 100.0)
        }
        return CGSize.zero

    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        if kind == UICollectionElementKindSectionFooter {

            let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer", for: indexPath)


            let loading = UIActivityIndicatorView()
            loading.activityIndicatorViewStyle = .gray
            loading.translatesAutoresizingMaskIntoConstraints = false
            loading.tintColor = UIColor.gray
            loading.tag = -123456
            view.addSubview(loading)
            view.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0))
            vew.addConstraint(NSLayoutConstraint(item: loading, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0))


            return view
        }
        return UICollectionReusableView()
    }

    func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
        if elementKind == UICollectionElementKindSectionFooter {

            if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
                if arrData.count > 0 && isLastPageReached == false 
                {
                    loadingView.startAnimating()
                    self.loadMore()
                }
                else
                {
                    self.isLoadMore = false
                }
            }
        }
    }
    func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {
        if elementKind == UICollectionElementKindSectionFooter{

            if let loadingView = view.viewWithTag(-123456) as? UIActivityIndicatorView{
                loadingView.stopAnimating()
                loadingView.removeFromSuperview()

                self.isLoadMore = false
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)