Skip to content

Commit

Permalink
add function to force update emptyDataSet
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed May 4, 2016
1 parent d0c9d09 commit d38d7fd
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 12 deletions.
33 changes: 29 additions & 4 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<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">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--Empty Data Demo Table View Controller-->
Expand Down Expand Up @@ -35,12 +35,37 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="AN4-8s-pSk">
<size key="itemSize" width="50" height="50"/>
<size key="itemSize" width="160" height="118"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
</collectionViewFlowLayout>
<cells/>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="Cell" id="hIW-pB-eH7">
<rect key="frame" x="0.0" y="64" width="160" height="118"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="160" height="118"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<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">
<rect key="frame" x="0.0" y="48" width="160" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.2196078431372549" green="0.2196078431372549" blue="0.2196078431372549" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
<color key="backgroundColor" red="0.86611149850000002" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="BEj-6c-73Q" firstAttribute="leading" secondItem="hIW-pB-eH7" secondAttribute="leading" id="JOd-4d-wJP"/>
<constraint firstAttribute="trailing" secondItem="BEj-6c-73Q" secondAttribute="trailing" id="dv6-Kt-w7I"/>
<constraint firstItem="BEj-6c-73Q" firstAttribute="centerY" secondItem="hIW-pB-eH7" secondAttribute="centerY" id="qrx-KL-dJl"/>
<constraint firstItem="BEj-6c-73Q" firstAttribute="centerX" secondItem="hIW-pB-eH7" secondAttribute="centerX" id="uh9-Xc-Hvo"/>
</constraints>
</collectionViewCell>
</cells>
<connections>
<outlet property="dataSource" destination="FIk-rG-V0x" id="Ie5-4i-wFd"/>
<outlet property="delegate" destination="FIk-rG-V0x" id="p57-cM-0vu"/>
Expand Down
56 changes: 52 additions & 4 deletions Example/EmptyDataDemoCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
// MARK: - Properties
var indexPath = NSIndexPath()
private var isLoading = false
private var dataCount = 0

// MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()

navigationItem.title = "CollectionView"
collectionView!.backgroundColor = UIColor.whiteColor()

collectionView!.emptyDataSetDataSource = self
collectionView!.emptyDataSetDelegate = self
collectionView!.backgroundColor = UIColor.whiteColor()
collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: CellIdentifier.reuseIdentifier)

if indexPath.row != 0 {
loadData(self)
Expand All @@ -39,26 +40,44 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
isLoading = true
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in
self.dataCount = 4
self.isLoading = false
self.collectionView!.reloadData()
}
}

// MARK: - Collection view data source
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 0
return 1
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 0
return dataCount
}

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

let maskLayer = CAShapeLayer()
let maskRect = cell.bounds
maskLayer.frame = maskRect
let cornerRadii = CGSize(width: 5, height: 5)
let maskPath = UIBezierPath(roundedRect: maskRect, byRoundingCorners: .AllCorners, cornerRadii: cornerRadii)
maskLayer.path = maskPath.CGPath
cell.layer.mask = maskLayer

return cell
}

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.dataCount -= 1
collectionView.performBatchUpdates({
self.collectionView?.deleteItemsAtIndexPaths([indexPath])
}) { (finished) in
self.collectionView?.updateEmptyDataSetIfNeeded()
}
}

// MARK: - TBEmptyDataSet data source
func titleForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString? {
let title = EmptyData.titles[indexPath.row]
Expand Down Expand Up @@ -160,3 +179,32 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
presentViewController(alert, animated: true, completion: nil)
}
}

extension EmptyDataDemoCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 150, height: 90)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(20, 20, 20, 20)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 20
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 20
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

collectionViewLayout.invalidateLayout()
coordinator.animateAlongsideTransition({ (context) -> Void in

}) { (context) -> Void in

}
}
}
4 changes: 2 additions & 2 deletions Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.6</string>
<string>1.7</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>7</string>
<string>8</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ func emptyDataSetDidDisappear(scrollView: UIScrollView!) {
// do something
}
```
##### 3. Data source events (inserting, deleting, and reloading)
TBEmptyDataSet will update automatically when the data source of table view or collection view changes.

Specifically, for UITableView, it updates when ```endUpdates()``` is called, for both UITableView and UICollectionView, it updates when ```reloadData()``` is called.

In addition, you can call ```updateEmptyDataSetIfNeeded()``` to force an update immediately. For example:
```swift
collectionView.performBatchUpdates({
self.collectionView?.deleteItemsAtIndexPaths([indexPath])
}) { (finished) in
self.collectionView?.updateEmptyDataSetIfNeeded()
}
```

## Minimum Requirement
iOS 8.0
Expand Down
2 changes: 1 addition & 1 deletion TBEmptyDataSet.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

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

s.homepage = "https://github.com/teambition/TBEmptyDataSet"
Expand Down
2 changes: 1 addition & 1 deletion TBEmptyDataSet/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.6</string>
<string>1.7</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
5 changes: 5 additions & 0 deletions TBEmptyDataSet/TBEmptyDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ extension UIScrollView: UIGestureRecognizerDelegate {
return emptyDataSetDelegate?.emptyDataSetScrollEnabled(self) ?? false
}

// MARK: - Public
public func updateEmptyDataSetIfNeeded() {
reloadEmptyDataSet()
}

// MARK: - View events
func didTapEmptyDataView(sender: AnyObject) {
emptyDataSetDelegate?.emptyDataSetDidTapView(self)
Expand Down

0 comments on commit d38d7fd

Please sign in to comment.