我已经在我的一个应用程序中添加了加载更多功能,所以让我给你代码
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
}
}
}