Library of the Month: DZNEmptyDataSet

This month we’re checking out the DZNEmptyDataSet library which can be included in your XCode project via CocoaPods.

If your UICollectionView or UITableView views are populated dynamically, this library will make it extremely easy to handle the case where these views might be empty if there is a situation where there isn’t any available data to display.  Adding these little touches to your application really gives it that complete feel.

The best part is that setting this up only requires minimal additional coding on your part to get a pretty sleek looking empty set.  Plus, you can simply add it to an existing UITableView or UICollectionView that you’ve already been using in your project without any hassle.

Here’s a little example I’ve put together with a UITableView just to show you how easy it truly is.

I’ve added mine using CocoaPods as follows…

pod 'DZNEmptyDataSet'

I’ve just set up a basic UITableView in storyboard and connected it to my ViewController.swift class.  To start using this library, we’ll import DZNEmptyDataSet and adopt it.  This will look like…

import DZNEmptyDataSet

class ViewController: UIViewController, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { ... }

Here’s what my class looks like after adding in the following datasource and delegate methods…

import UIKit
import DZNEmptyDataSet
class ViewController: UIViewController, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
@IBOutlet var tableView: UITableView!
let tungsten: UIColor = UIColor(red:0.20, green:0.20, blue:0.20, alpha:1.0)
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.emptyDataSetSource = self
self.tableView.emptyDataSetDelegate = self
self.tableView.tableFooterView = UIView() // remove empty cells from tableView
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Add an image to your empty data set
func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
let image = UIImage(named: "Coffee")?.withRenderingMode(.alwaysTemplate)
scrollView.tintColor = self.tungsten
return image
}
// Add a title to your empty data set
func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
let text = "Sorry, nothing found here."
let attribs = [
NSFontAttributeName: UIFont(name: "Helvetica", size: 18.0)!,
NSForegroundColorAttributeName: self.tungsten
]
return NSAttributedString(string: text, attributes: attribs)
}
// Add a description to your empty data set
func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
let text = "Please check back at another time and we might have something more."
let para = NSMutableParagraphStyle()
para.lineBreakMode = NSLineBreakMode.byWordWrapping
para.alignment = NSTextAlignment.center
let attribs = [
NSFontAttributeName: UIFont(name: "Helvetica", size: 14.0)!,
NSForegroundColorAttributeName: UIColor.lightGray,
NSParagraphStyleAttributeName: para
]
return NSAttributedString(string: text, attributes: attribs)
}
// Set the background color of your empty data set
func backgroundColor(forEmptyDataSet scrollView: UIScrollView!) -> UIColor! {
return UIColor(red:0.96, green:0.96, blue:0.96, alpha:1.0)
}
}

And if we run our application, we’ll see our cool empty data set result!

Screen Shot 2017-10-22 at 10.30.29 AM.png

If you enjoyed this segment or if you have any library suggestions you would like for me to check out, I’d love to hear about them – leave a comment below!

Also, shoutout to Outlane on www.dribbble.com for the illustration used in this post.  If you’re looking for some great free resources to add to your application, make sure to check out http://freebbble.com/.