Skip to content

Instantly share code, notes, and snippets.

@dannolan
Created September 29, 2014 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannolan/7bf199e5db2fe09bd821 to your computer and use it in GitHub Desktop.
Save dannolan/7bf199e5db2fe09bd821 to your computer and use it in GitHub Desktop.
Generic TVDS in Swift
//
// GenericDataSource.swift
//
// Created by Dan Nolan on 23/09/2014.
// Creative Commons blah blah just use this as however you want I release all witchcraft and magic rights to using this software or something
//
import Foundation
import UIKit
class GenericDataSource: NSObject, UITableViewDelegate, UITableViewDataSource {
var items:[AnyObject] = []
var selectedCell: (object: AnyObject?) -> ()?
var configureCell:(cell: UITableViewCell, object: AnyObject?) -> ()?
var cellIdentifier = ""
init(items: [AnyObject], identifier: String, selectCellBlock: (AnyObject? -> ()), configureCellBlock: ((cell: UITableViewCell, object: AnyObject?) -> ())){
self.items = items
self.selectedCell = selectCellBlock
self.configureCell = configureCellBlock
self.cellIdentifier = identifier
super.init()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier, forIndexPath: indexPath) as UITableViewCell;
self.configureCell(cell: cell, object: self.items[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
self.selectedCell(object: self.items[indexPath.row]);
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44.0
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment