Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Last active December 16, 2017 07:51
Show Gist options
  • Save wh1pch81n/1f94ac19b165d4a3f514a2d024d304b6 to your computer and use it in GitHub Desktop.
Save wh1pch81n/1f94ac19b165d4a3f514a2d024d304b6 to your computer and use it in GitHub Desktop.
Sometimes it is easier to make a tableview generic. And just load it with info, rather than creating a whole class that conforms with UITableViewDataSource and UITableViewDelegate
import UIKit
class MyTableViewCell: UITableViewCell {
@IBOutlet var switchview: UISwitch!
@IBOutlet var sliderview: UISlider!
}
class Section {
var count: Int { return cells.count }
var cells = [Cell]()
var headerTitle = String()
}
class Cell {
var tableViewCellType = UITableViewCell.self
var tableViewCellForRowAtIndexPath: (UITableView, IndexPath) -> UITableViewCell = { _ in UITableViewCell() }
// func tableView<T: UITableViewCell>(_ : UITableView, cell:TForRowAtIndexPath)
}
extension UITableView {
func dequeueReusableCell<T: UITableViewCell>(_ type: T.Type, withIdentifier identifier: String = "", for indexPath: IndexPath) -> T {
return dequeueReusableCell(withIdentifier: identifier.isEmpty ? String(T.self) : identifier
, for: indexPath) as! T
}
}
class GenericTableView: UITableView, UITableViewDataSource, UITableViewDelegate {
var dataSourceArray: [Section] = []
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
setUp()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
func setUp() {
self.dataSource = self
self.delegate = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let _cell = dataSourceArray[indexPath.section].cells[indexPath.row]
return _cell.tableViewCellForRowAtIndexPath(tableView, indexPath)
}
func numberOfSections(in tableView: UITableView) -> Int {
return dataSourceArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSourceArray[section].count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let title = dataSourceArray[section].headerTitle
return (title.isEmpty) ? nil : title
}
}
class CoolViewController: UIViewController {
@IBOutlet weak var tableView: GenericTableView!
override func viewDidLoad() {
tableView.dataSourceArray = [
{
$0.headerTitle = "Section 1"
$0.cells = [
{
let cellTypeIdentifier = String($0.tableViewCellType)
$0.tableViewCellForRowAtIndexPath = {
let cell = $0.dequeueReusableCell(withIdentifier: cellTypeIdentifier, for: $1)
cell.textLabel!.text = String($1)
return cell
}
return $0
}(Cell()),
{
$0.tableViewCellType = MyTableViewCell.self
let cellTypeIdentifier = String($0.tableViewCellType)
$0.tableViewCellForRowAtIndexPath = {
let cell = $0.dequeueReusableCell(withIdentifier: cellTypeIdentifier, for: $1) as! MyTableViewCell
cell.switchview.isOn = true
return cell
}
return $0
}(Cell()),
{
$0.tableViewCellType = MyTableViewCell.self
$0.tableViewCellForRowAtIndexPath = {
let cell = $0.dequeueReusableCell(MyTableViewCell.self, for: $1)
cell.switchview.isOn = false
cell.sliderview.value = 0.9
return cell
}
return $0
}(Cell())
]
return $0
}(Section())
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment