Skip to content

Commit

Permalink
Merge pull request #20 from teambition/feature/support-tableView-perf…
Browse files Browse the repository at this point in the history
…ormBatchUpdates-handle-in-iOS11

feature: support tableView performBatchUpdates handle
  • Loading branch information
hongxinhope authored Nov 19, 2018
2 parents da1b36c + 8dbd13a commit 884e863
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ TBEmptyDataSet will update automatically when the data source of table view or c

To be specific:
* For UITableView, it updates when ```endUpdates()``` is called.
* For UICollectionView, it updates when ```performBatchUpdates(_:completion:)``` is completed.
* For both UITableView and UICollectionView, it updates when ```performBatchUpdates(_:completion:)``` is completed.
* For both UITableView and UICollectionView, it updates when ```reloadData()``` is called.

## Minimum Requirement
Expand Down
4 changes: 4 additions & 0 deletions TBEmptyDataSet/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal struct AssociatedKeys {
internal struct Selectors {
static let tableViewSwizzledReloadData = #selector(UIScrollView.tb_tableViewSwizzledReloadData)
static let tableViewSwizzledEndUpdates = #selector(UIScrollView.tb_tableViewSwizzledEndUpdates)
@available(iOS 11.0, *)
static let tableViewSwizzledPerformBatchUpdates = #selector(UIScrollView.tb_tableViewSwizzledPerformBatchUpdates(_:completion:))
static let collectionViewSwizzledReloadData = #selector(UIScrollView.tb_collectionViewSwizzledReloadData)
static let collectionViewSwizzledPerformBatchUpdates = #selector(UIScrollView.tb_collectionViewSwizzledPerformBatchUpdates(_:completion:))
}
Expand All @@ -26,6 +28,8 @@ internal struct TableViewSelectors {
static let reloadData = #selector(UITableView.reloadData)
static let endUpdates = #selector(UITableView.endUpdates)
static let numberOfSections = #selector(UITableViewDataSource.numberOfSections(in:))
@available(iOS 11.0, *)
static let performBatchUpdates = #selector(UITableView.performBatchUpdates(_:completion:))
}

internal struct CollectionViewSelectors {
Expand Down
21 changes: 21 additions & 0 deletions TBEmptyDataSet/TBEmptyDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ extension UIScrollView {
case is UITableView:
UITableView.tb_swizzleTableViewReloadData
UITableView.tb_swizzleTableViewEndUpdates
if #available(iOS 11.0, *) {
UITableView.tb_swizzleTableViewPerformBatchUpdates
}
case is UICollectionView:
UICollectionView.tb_swizzleCollectionViewReloadData
UICollectionView.tb_swizzleCollectionViewPerformBatchUpdates
Expand Down Expand Up @@ -277,6 +280,15 @@ extension UIScrollView {
tb_swizzleMethod(for: UITableView.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)
}()

// swiftlint:disable variable_name
@available(iOS 11.0, *)
fileprivate static let tb_swizzleTableViewPerformBatchUpdates: () = {
let originalSelector = TableViewSelectors.performBatchUpdates
let swizzledSelector = Selectors.tableViewSwizzledPerformBatchUpdates

tb_swizzleMethod(for: UITableView.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)
}()

// swiftlint:disable variable_name
fileprivate static let tb_swizzleCollectionViewReloadData: () = {
let originalSelector = CollectionViewSelectors.reloadData
Expand Down Expand Up @@ -305,6 +317,15 @@ extension UIScrollView {
reloadEmptyDataSet()
}

@available(iOS 11.0, *)
@objc
func tb_tableViewSwizzledPerformBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)?) {
tb_tableViewSwizzledPerformBatchUpdates(updates) { [weak self](completed) in
completion?(completed)
self?.reloadEmptyDataSet()
}
}

@objc
func tb_collectionViewSwizzledReloadData() {
tb_collectionViewSwizzledReloadData()
Expand Down

0 comments on commit 884e863

Please sign in to comment.