KissPaginate Simplify your implementation of tableView paginate.
To run the example project, clone the repo, and run pod install
from the Example directory first.
class ViewController: PaginateViewController {
@IBOutlet weak var noElementLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
refreshElements()
}
override var getElementsClosure: (page: Int, successHandler: GetElementsSuccessHandler, failureHandler: (error: Error) -> Void) -> Void {
return getElementList
}
func getElementList(page: Int, successHandler: GetElementsSuccessHandler, failureHandler: (error: Error) -> Void) {
let elements = (0...20).map { "page \(page), element index" + String($0) }
delay(2) {
successHandler(elements, true)
}
}
override func displayNoElementIfNeeded(noElement: Bool) {
noElementLabel.isHidden = !noElement
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
let element = getElement(String.self, at: (indexPath as NSIndexPath).row)
cell.textLabel?.text = element
if elements.count == (indexPath as NSIndexPath).row + 1 {
loadNextPage()
}
return cell
}
}
if your ViewController already has a parent class, you can inherit from the protocal PaginateView
class PaginateView: UIViewController, PaginateView {
var presenter: PaginatePresenter!
var refreshControl: UIRefreshControl!
var bottomRefresh: UIActivityIndicatorView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var noElementLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
presenter = PaginatePresenter(paginatable: self)
presenter.start()
tableView.dataSource = self
refreshElements()
}
var getElementsClosure: (page: Int, successHandler: GetElementsSuccessHandler, failureHandler: (error: Error) -> Void) -> Void {
return getElementList
}
func getElementList(page: Int, successHandler: GetElementsSuccessHandler, failureHandler: (error: Error) -> Void) {
let elements = (0...20).map { "page \(page), element index" + String($0) }
delay(2) {
successHandler(elements, true)
}
}
func displayNoElementIfNeeded(noElement: Bool) {
noElementLabel.isHidden = !noElement
}
}
extension PaginateView: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
let element = getElement(String.self, at: (indexPath as NSIndexPath).row)
cell.textLabel?.text = element
if elements.count == (indexPath as NSIndexPath).row + 1 {
loadNextPage()
}
return cell
}
}
KissPaginate is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'KissPaginate'
WANG Jie, [email protected]
KissPaginate is available under the MIT license. See the LICENSE file for more info.