Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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