From d38d7fd0d35cadd8ef190ba7890be933fb4b0579 Mon Sep 17 00:00:00 2001 From: Xin Hong Date: Wed, 4 May 2016 10:29:55 +0800 Subject: [PATCH] add function to force update emptyDataSet --- Example/Base.lproj/Main.storyboard | 33 +++++++++-- ...mptyDataDemoCollectionViewController.swift | 56 +++++++++++++++++-- Example/Info.plist | 4 +- README.md | 13 +++++ TBEmptyDataSet.podspec | 2 +- TBEmptyDataSet/Supporting Files/Info.plist | 2 +- TBEmptyDataSet/TBEmptyDataSet.swift | 5 ++ 7 files changed, 103 insertions(+), 12 deletions(-) diff --git a/Example/Base.lproj/Main.storyboard b/Example/Base.lproj/Main.storyboard index d6604a5..912eddf 100644 --- a/Example/Base.lproj/Main.storyboard +++ b/Example/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -35,12 +35,37 @@ - + - + + + + + + + + + + + + + + + + + + + + + diff --git a/Example/EmptyDataDemoCollectionViewController.swift b/Example/EmptyDataDemoCollectionViewController.swift index a2ca7be..395ae3c 100644 --- a/Example/EmptyDataDemoCollectionViewController.swift +++ b/Example/EmptyDataDemoCollectionViewController.swift @@ -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) @@ -39,6 +40,7 @@ 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() } @@ -46,19 +48,36 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty // 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] @@ -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 + + } + } +} diff --git a/Example/Info.plist b/Example/Info.plist index 41aac54..a48c34c 100644 --- a/Example/Info.plist +++ b/Example/Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.6 + 1.7 CFBundleSignature ???? CFBundleVersion - 7 + 8 LSRequiresIPhoneOS UILaunchStoryboardName diff --git a/README.md b/README.md index 0cd3700..b68f78d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/TBEmptyDataSet.podspec b/TBEmptyDataSet.podspec index 39c380c..f7b0c78 100644 --- a/TBEmptyDataSet.podspec +++ b/TBEmptyDataSet.podspec @@ -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" diff --git a/TBEmptyDataSet/Supporting Files/Info.plist b/TBEmptyDataSet/Supporting Files/Info.plist index 6c0ca92..c5f1415 100644 --- a/TBEmptyDataSet/Supporting Files/Info.plist +++ b/TBEmptyDataSet/Supporting Files/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.6 + 1.7 CFBundleSignature ???? CFBundleVersion diff --git a/TBEmptyDataSet/TBEmptyDataSet.swift b/TBEmptyDataSet/TBEmptyDataSet.swift index 9628a8f..3b03b8a 100644 --- a/TBEmptyDataSet/TBEmptyDataSet.swift +++ b/TBEmptyDataSet/TBEmptyDataSet.swift @@ -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)