Skip to content

Instantly share code, notes, and snippets.

@imnaveensharma
Created July 27, 2020 10:04
Show Gist options
  • Save imnaveensharma/0cb64dcccd677afe3c8c27469dd791a4 to your computer and use it in GitHub Desktop.
Save imnaveensharma/0cb64dcccd677afe3c8c27469dd791a4 to your computer and use it in GitHub Desktop.
Add "Pull to Refresh" and "Load More" on UITableView and UICollectionView
//Step 2: Install below given Pod
pod 'CCBottomRefreshControl'
//Step 2: Add below given line in bridging header file
#import <CCBottomRefreshControl/UIScrollView+BottomRefreshControl.h>
//Step 3:
// MARK: - Properties ::
private var pullToRefreshControl: UIRefreshControl = UIRefreshControl()
private var loadMoreRefreshControl: UIRefreshControl = UIRefreshControl()
private var pageNumber = 0
private var isDataLoading = false
private var isAllDataLoaded = false
override func viewDidLoad() {
super.viewDidLoad()
//Step 4:
self.addPullToRefresh()
self.addLoadMore()
}
private func addPullToRefresh() {
self.pullToRefreshControl.addTarget(self, action: #selector(pullToRefresh(refreshControl:)), for: .valueChanged)
self.collectionView.addSubview(self.pullToRefreshControl)
//self.table.addSubview(self.pullToRefreshControl)
}
private func addLoadMore() {
self.loadMoreRefreshControl.triggerVerticalOffset = 100
self.loadMoreRefreshControl.addTarget(self, action: #selector(loadMore(refreshControl:)), for: .valueChanged)
self.collectionView.bottomRefreshControl = self.loadMoreRefreshControl
//self.tableView.bottomRefreshControl = self.loadMoreRefreshControl
}
// MARK: - Selectors Actions ::
@objc func pullToRefresh(refreshControl: UIRefreshControl) {
//Reset All Data Sources
//Call API
}
@objc func loadMore(refreshControl: UIRefreshControl) {
if !self.isDataLoading && !self.isAllDataLoaded {
//Call API for More Data
} else {
self.loadMoreRefreshControl.endRefreshing()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment