Skip to content

Commit

Permalink
Merge pull request #9 from salmaanahmed/Bug-fixes-and-additional-feat…
Browse files Browse the repository at this point in the history
…ures

Bug fixes and additional features
  • Loading branch information
salmaanahmed authored Dec 30, 2019
2 parents fe2f273 + 548aa0d commit e9e4504
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
26 changes: 25 additions & 1 deletion Example/PaginatedTableView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ViewController: UIViewController {

// More settings
tableView.enablePullToRefresh = true
tableView.pullToRefreshTitle = NSAttributedString(string: "Pull to Refresh")

tableView.loadData(refresh: true)
}
Expand All @@ -37,7 +38,7 @@ class ViewController: UIViewController {
}

//
// MARK Paginated Delegate - Where magic happens
// MARK: Paginated Delegate - Where magic happens
//
extension ViewController: PaginatedTableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Expand Down Expand Up @@ -81,4 +82,27 @@ extension ViewController: PaginatedTableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
print(scrollView.contentOffset.y)
}
}

//
// MARK: Enable swipe in paginatedTableView
//
extension ViewController {
// For Swipe Actions
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

}

// To enable swipe, make sure you overide this method
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if self.list[indexPath.row] % 3 == 0 {
return .delete
} else {
return .none
}
}
}
2 changes: 1 addition & 1 deletion PaginatedTableView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'PaginatedTableView'
s.version = '0.1.0'
s.version = '1.0'
s.summary = 'Assign this class to your UITableView and it will be equipped with pull to refresh as well as infinite scroll'

# This description is used to generate tags and improve search results.
Expand Down
23 changes: 21 additions & 2 deletions PaginatedTableView/Classes/PaginatedTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import Foundation
import UIKit

public protocol PaginatedTableViewDataSource: class {
@objc public protocol PaginatedTableViewDataSource: class {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func numberOfSections(in tableView: UITableView) -> Int
@objc optional func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
@objc optional func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
}

@objc public protocol PaginatedTableViewDelegate: class {
Expand All @@ -24,6 +26,7 @@ public protocol PaginatedTableViewDataSource: class {
func loadMore(_ pageNumber: Int, _ pageSize: Int, onSuccess: ((Bool) -> Void)?, onError: ((Error) -> Void)?)
@objc optional func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
@objc optional func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
@objc optional func scrollViewDidScroll(_ scrollView: UIScrollView)
}

//
Expand Down Expand Up @@ -51,6 +54,12 @@ public class PaginatedTableView: UITableView {
public var heightForHeaderInSection: CGFloat = 0
public var titleForHeaderInSection = ""

public var pullToRefreshTitle: NSAttributedString? = nil {
didSet {
refreshControltableView.attributedTitle = pullToRefreshTitle
}
}

public var enablePullToRefresh = false {
willSet {
if newValue == enablePullToRefresh { return }
Expand All @@ -69,7 +78,7 @@ public class PaginatedTableView: UITableView {
// refresh control
lazy var refreshControltableView: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.attributedTitle = pullToRefreshTitle
refreshControl.addTarget(self, action: #selector(self.handleRefreshtableView(_:)), for: UIControl.Event.valueChanged)
return refreshControl
}()
Expand Down Expand Up @@ -157,6 +166,8 @@ extension PaginatedTableView {
if distanceFromBottom < height {
load()
}

paginatedDelegate?.scrollViewDidScroll?(scrollView)
}
}

Expand Down Expand Up @@ -245,6 +256,14 @@ extension PaginatedTableView: UITableViewDataSource, UITableViewDelegate {
}
return paginatedDelegate?.tableView(tableView, heightForRowAt: indexPath) ?? 0
}

public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
paginatedDataSource?.tableView?(tableView, commit: editingStyle, forRowAt: indexPath)
}

public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return paginatedDataSource?.tableView?(tableView, editingStyleForRowAt: indexPath) ?? .none
}
}

//
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Guess what, it comes with pull to refresh by default ;)
onSuccess?(moreDataAvailable)
}
```
The latest release supports swiping cells and listening to scroll changes, please see example for more features.

**Step 5: Enjoy**
Yeah! Thats all. You now have paginated table view with infinite scroll along with pull to refresh :heart:
Expand Down

0 comments on commit e9e4504

Please sign in to comment.