diff --git a/README.md b/README.md index 60e2767..e86875c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/TBEmptyDataSet/Constants.swift b/TBEmptyDataSet/Constants.swift index 9c0782c..bdb1f7e 100644 --- a/TBEmptyDataSet/Constants.swift +++ b/TBEmptyDataSet/Constants.swift @@ -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:)) } @@ -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 { diff --git a/TBEmptyDataSet/TBEmptyDataSet.swift b/TBEmptyDataSet/TBEmptyDataSet.swift index 327424d..43d2b57 100644 --- a/TBEmptyDataSet/TBEmptyDataSet.swift +++ b/TBEmptyDataSet/TBEmptyDataSet.swift @@ -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 @@ -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 @@ -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()