Skip to content

Instantly share code, notes, and snippets.

@dannolan dannolan/tvds.swift
Created Sep 29, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.