Skip to content

Commit d38d7fd

Browse files
committed
add function to force update emptyDataSet
1 parent d0c9d09 commit d38d7fd

File tree

7 files changed

+103
-12
lines changed

7 files changed

+103
-12
lines changed

Example/Base.lproj/Main.storyboard

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="kbE-4i-otZ">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="kbE-4i-otZ">
33
<dependencies>
44
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
66
</dependencies>
77
<scenes>
88
<!--Empty Data Demo Table View Controller-->
@@ -35,12 +35,37 @@
3535
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
3636
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
3737
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="AN4-8s-pSk">
38-
<size key="itemSize" width="50" height="50"/>
38+
<size key="itemSize" width="160" height="118"/>
3939
<size key="headerReferenceSize" width="0.0" height="0.0"/>
4040
<size key="footerReferenceSize" width="0.0" height="0.0"/>
4141
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
4242
</collectionViewFlowLayout>
43-
<cells/>
43+
<cells>
44+
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="Cell" id="hIW-pB-eH7">
45+
<rect key="frame" x="0.0" y="64" width="160" height="118"/>
46+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
47+
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
48+
<rect key="frame" x="0.0" y="0.0" width="160" height="118"/>
49+
<autoresizingMask key="autoresizingMask"/>
50+
<subviews>
51+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Click to delete" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="BEj-6c-73Q">
52+
<rect key="frame" x="0.0" y="48" width="160" height="21"/>
53+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
54+
<color key="textColor" red="0.2196078431372549" green="0.2196078431372549" blue="0.2196078431372549" alpha="1" colorSpace="calibratedRGB"/>
55+
<nil key="highlightedColor"/>
56+
</label>
57+
</subviews>
58+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
59+
</view>
60+
<color key="backgroundColor" red="0.86611149850000002" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
61+
<constraints>
62+
<constraint firstItem="BEj-6c-73Q" firstAttribute="leading" secondItem="hIW-pB-eH7" secondAttribute="leading" id="JOd-4d-wJP"/>
63+
<constraint firstAttribute="trailing" secondItem="BEj-6c-73Q" secondAttribute="trailing" id="dv6-Kt-w7I"/>
64+
<constraint firstItem="BEj-6c-73Q" firstAttribute="centerY" secondItem="hIW-pB-eH7" secondAttribute="centerY" id="qrx-KL-dJl"/>
65+
<constraint firstItem="BEj-6c-73Q" firstAttribute="centerX" secondItem="hIW-pB-eH7" secondAttribute="centerX" id="uh9-Xc-Hvo"/>
66+
</constraints>
67+
</collectionViewCell>
68+
</cells>
4469
<connections>
4570
<outlet property="dataSource" destination="FIk-rG-V0x" id="Ie5-4i-wFd"/>
4671
<outlet property="delegate" destination="FIk-rG-V0x" id="p57-cM-0vu"/>

Example/EmptyDataDemoCollectionViewController.swift

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
1818
// MARK: - Properties
1919
var indexPath = NSIndexPath()
2020
private var isLoading = false
21+
private var dataCount = 0
2122

2223
// MARK: - View life cycle
2324
override func viewDidLoad() {
2425
super.viewDidLoad()
2526

2627
navigationItem.title = "CollectionView"
28+
collectionView!.backgroundColor = UIColor.whiteColor()
29+
2730
collectionView!.emptyDataSetDataSource = self
2831
collectionView!.emptyDataSetDelegate = self
29-
collectionView!.backgroundColor = UIColor.whiteColor()
30-
collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: CellIdentifier.reuseIdentifier)
3132

3233
if indexPath.row != 0 {
3334
loadData(self)
@@ -39,26 +40,44 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
3940
isLoading = true
4041
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
4142
dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in
43+
self.dataCount = 4
4244
self.isLoading = false
4345
self.collectionView!.reloadData()
4446
}
4547
}
4648

4749
// MARK: - Collection view data source
4850
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
49-
return 0
51+
return 1
5052
}
5153

5254
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
53-
return 0
55+
return dataCount
5456
}
5557

5658
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
5759
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CellIdentifier.reuseIdentifier, forIndexPath: indexPath)
5860

61+
let maskLayer = CAShapeLayer()
62+
let maskRect = cell.bounds
63+
maskLayer.frame = maskRect
64+
let cornerRadii = CGSize(width: 5, height: 5)
65+
let maskPath = UIBezierPath(roundedRect: maskRect, byRoundingCorners: .AllCorners, cornerRadii: cornerRadii)
66+
maskLayer.path = maskPath.CGPath
67+
cell.layer.mask = maskLayer
68+
5969
return cell
6070
}
6171

72+
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
73+
self.dataCount -= 1
74+
collectionView.performBatchUpdates({
75+
self.collectionView?.deleteItemsAtIndexPaths([indexPath])
76+
}) { (finished) in
77+
self.collectionView?.updateEmptyDataSetIfNeeded()
78+
}
79+
}
80+
6281
// MARK: - TBEmptyDataSet data source
6382
func titleForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString? {
6483
let title = EmptyData.titles[indexPath.row]
@@ -160,3 +179,32 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
160179
presentViewController(alert, animated: true, completion: nil)
161180
}
162181
}
182+
183+
extension EmptyDataDemoCollectionViewController: UICollectionViewDelegateFlowLayout {
184+
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
185+
return CGSize(width: 150, height: 90)
186+
}
187+
188+
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
189+
return UIEdgeInsetsMake(20, 20, 20, 20)
190+
}
191+
192+
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
193+
return 20
194+
}
195+
196+
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
197+
return 20
198+
}
199+
200+
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
201+
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
202+
203+
collectionViewLayout.invalidateLayout()
204+
coordinator.animateAlongsideTransition({ (context) -> Void in
205+
206+
}) { (context) -> Void in
207+
208+
}
209+
}
210+
}

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.6</string>
18+
<string>1.7</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>7</string>
22+
<string>8</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UILaunchStoryboardName</key>

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ func emptyDataSetDidDisappear(scrollView: UIScrollView!) {
9999
// do something
100100
}
101101
```
102+
##### 3. Data source events (inserting, deleting, and reloading)
103+
TBEmptyDataSet will update automatically when the data source of table view or collection view changes.
104+
105+
Specifically, for UITableView, it updates when ```endUpdates()``` is called, for both UITableView and UICollectionView, it updates when ```reloadData()``` is called.
106+
107+
In addition, you can call ```updateEmptyDataSetIfNeeded()``` to force an update immediately. For example:
108+
```swift
109+
collectionView.performBatchUpdates({
110+
self.collectionView?.deleteItemsAtIndexPaths([indexPath])
111+
}) { (finished) in
112+
self.collectionView?.updateEmptyDataSetIfNeeded()
113+
}
114+
```
102115

103116
## Minimum Requirement
104117
iOS 8.0

TBEmptyDataSet.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "TBEmptyDataSet"
4-
s.version = "1.6"
4+
s.version = "1.7"
55
s.summary = "An extension of UITableView/UICollectionView's super class, it will display a placeholder when the data is empty."
66

77
s.homepage = "https://github.com/teambition/TBEmptyDataSet"

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.6</string>
18+
<string>1.7</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

TBEmptyDataSet/TBEmptyDataSet.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ extension UIScrollView: UIGestureRecognizerDelegate {
145145
return emptyDataSetDelegate?.emptyDataSetScrollEnabled(self) ?? false
146146
}
147147

148+
// MARK: - Public
149+
public func updateEmptyDataSetIfNeeded() {
150+
reloadEmptyDataSet()
151+
}
152+
148153
// MARK: - View events
149154
func didTapEmptyDataView(sender: AnyObject) {
150155
emptyDataSetDelegate?.emptyDataSetDidTapView(self)

0 commit comments

Comments
 (0)