Skip to content

Instantly share code, notes, and snippets.

@cmoulton
Last active February 21, 2020 04:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cmoulton/5c06cc57d525e377f65aabacd07e21a8 to your computer and use it in GitHub Desktop.
Save cmoulton/5c06cc57d525e377f65aabacd07e21a8 to your computer and use it in GitHub Desktop.
Using DZNEmptyDataSet in a UIViewController
//
// DetailViewController.swift
// objcInterop
//
// Created by Christina Moulton on 2015-07-02.
// Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//
import UIKit
import DZNEmptyDataSet
class DetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
@IBOutlet weak var tableView: UITableView?
var objects = [AnyObject]()
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
tableView?.emptyDataSetSource = self
tableView?.emptyDataSetDelegate = self
// A little trick for removing the cell separators
self.tableView?.tableFooterView = UIView()
}
deinit {
self.tableView?.emptyDataSetSource = nil
self.tableView?.emptyDataSetDelegate = nil
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table View
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let object = objects[indexPath.row] as! NSDate
cell.textLabel!.text = object.description
return cell
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
objects.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
self.tableView?.reloadEmptyDataSet()
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
// MARK: - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
func titleForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString! {
return NSAttributedString(string: "Got nothin'")
}
func descriptionForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString! {
return NSAttributedString(string: "Sorry about this, I'm just all out of data")
}
func emptyDataSetShouldDisplay(scrollView: UIScrollView!) -> Bool {
return objects.count == 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment