Skip to content

Commit fae5495

Browse files
committed
optimize collection view empty data set
1 parent 13730af commit fae5495

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

Example/EmptyDataDemoCollectionViewController.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
7373
self.dataCount -= 1
7474
collectionView.performBatchUpdates({
7575
self.collectionView?.deleteItemsAtIndexPaths([indexPath])
76-
}) { (finished) in
77-
self.collectionView?.updateEmptyDataSetIfNeeded()
76+
}) { (completed) in
77+
print("completed successfully: \(completed)")
78+
// self.collectionView?.updateEmptyDataSetIfNeeded()
7879
}
7980
}
8081

Example/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.9</string>
18+
<string>2.0</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>10</string>
22+
<string>11</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UILaunchStoryboardName</key>

TBEmptyDataSet/Constants.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import UIKit
1010

11+
// swiftlint:disable variable_name
1112
struct AssociatedKeys {
1213
static var emptyDataSetDataSource = "emptyDataSetDataSource"
1314
static var emptyDataSetDelegate = "emptyDataSetDelegate"
@@ -18,6 +19,7 @@ struct Selectors {
1819
static let tableViewSwizzledReloadData = #selector(UIScrollView.tb_tableViewSwizzledReloadData)
1920
static let tableViewSwizzledEndUpdates = #selector(UIScrollView.tb_tableViewSwizzledEndUpdates)
2021
static let collectionViewSwizzledReloadData = #selector(UIScrollView.tb_collectionViewSwizzledReloadData)
22+
static let collectionViewSwizzledPerformBatchUpdates = #selector(UIScrollView.tb_collectionViewSwizzledPerformBatchUpdates(_:completion:))
2123
}
2224

2325
struct TableViewSelectors {
@@ -29,6 +31,7 @@ struct TableViewSelectors {
2931
struct CollectionViewSelectors {
3032
static let reloadData = #selector(UICollectionView.reloadData)
3133
static let numberOfSections = #selector(UICollectionViewDataSource.numberOfSectionsInCollectionView(_:))
34+
static let performBatchUpdates = #selector(UICollectionView.performBatchUpdates(_:completion:))
3235
}
3336

3437
struct DefaultValues {

TBEmptyDataSet/Supporting Files/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.9</string>
18+
<string>2.0</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

TBEmptyDataSet/TBEmptyDataSet.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extension UIScrollView: UIGestureRecognizerDelegate {
5353
}
5454
if self is UICollectionView {
5555
UICollectionView.tb_swizzleCollectionViewReloadData()
56+
UICollectionView.tb_swizzleCollectionViewPerformBatchUpdates()
5657
}
5758
} else {
5859
handlingInvalidEmptyDataSet()
@@ -347,6 +348,19 @@ extension UIScrollView: UIGestureRecognizerDelegate {
347348
}
348349
}
349350

351+
private class func tb_swizzleCollectionViewPerformBatchUpdates() {
352+
struct EmptyDataSetSwizzleToken {
353+
static var onceToken: dispatch_once_t = 0
354+
}
355+
dispatch_once(&EmptyDataSetSwizzleToken.onceToken) {
356+
let originalSelector = CollectionViewSelectors.performBatchUpdates
357+
let swizzledSelector = Selectors.collectionViewSwizzledPerformBatchUpdates
358+
359+
tb_swizzleMethod(originalSelector, swizzledSelector: swizzledSelector)
360+
print(#function)
361+
}
362+
}
363+
350364
func tb_tableViewSwizzledReloadData() {
351365
tb_tableViewSwizzledReloadData()
352366
reloadEmptyDataSet()
@@ -361,4 +375,11 @@ extension UIScrollView: UIGestureRecognizerDelegate {
361375
tb_collectionViewSwizzledReloadData()
362376
reloadEmptyDataSet()
363377
}
378+
379+
func tb_collectionViewSwizzledPerformBatchUpdates(updates: (() -> Void)?, completion: ((Bool) -> Void)?) {
380+
tb_collectionViewSwizzledPerformBatchUpdates(updates) { [weak self](completed) in
381+
completion?(completed)
382+
self?.reloadEmptyDataSet()
383+
}
384+
}
364385
}

0 commit comments

Comments
 (0)