-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathConfigurableTableViewController.swift
More file actions
65 lines (51 loc) · 1.89 KB
/
ConfigurableTableViewController.swift
File metadata and controls
65 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// File.swift
// ConfigurableTableViewController
//
// Created by Arkadiusz Holko on 03-01-16.
// Copyright © 2016 Arkadiusz Holko. All rights reserved.
//
import UIKit
class ConfigurableTableViewController: UIViewController {
weak var tableView: UITableView!
var items: [SectionConfigurator]
init(items: [SectionConfigurator]) {
self.items = items
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: view.bounds, style: .Grouped)
tableView.tableFooterView = UIView()
tableView.rowHeight = 100
view.addSubview(tableView)
self.tableView = tableView
tableView.dataSource = self
registerCells()
}
func registerCells() {
for sectionConfigurator in items {
for cellConfigurator in sectionConfigurator.cellConfigurators {
tableView.registerClass(cellConfigurator.cellClass, forCellReuseIdentifier: cellConfigurator.reuseIdentifier)
}
}
}
}
extension ConfigurableTableViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return items.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].cellConfigurators.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let sectionConfigurator = items[indexPath.section]
let cellConfigurator = sectionConfigurator.cellConfigurators[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier(cellConfigurator.reuseIdentifier, forIndexPath: indexPath)
cellConfigurator.updateCell(cell)
return cell
}
}