Last active
September 8, 2017 18:33
-
-
Save joshuawright11/f9676894cb4143343a169df7583b0866 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Base static class to setup functionality and any custom design across all static views | |
| class StaticTableViewBase: UITableViewController { | |
| private var sections: [TableViewSection] = [] | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| tableView.register(BasicCell.self) | |
| tableView.register(FancyCell.self) | |
| tableView.register(UINib(nibName: "MyHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: MyHeaderView.cellIdentifier) | |
| sections = generateData() | |
| } | |
| // MARK: UITableView Delegate and DataSource Methods | |
| override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
| let row = sections[indexPath.section].rows[indexPath.row] | |
| let c = tableView.dequeueReusableCell(withIdentifier: row.type.cellIdentifier, for: indexPath) | |
| row.cellConfiguration?(c) | |
| return c | |
| } | |
| override func numberOfSections(in tableView: UITableView) -> Int { | |
| return sections.count | |
| } | |
| override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
| return 67.0 // or whatever, could make cells autosizing via autolayout | |
| } | |
| override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
| let headerView = MyHeaderView() | |
| headerView.title = sections[section].title | |
| return headerView | |
| } | |
| override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { | |
| return tableView.dequeueReusableHeaderFooterView(withIdentifier: MenuFooterView.cellIdentifier) | |
| } | |
| override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
| return 40 | |
| } | |
| override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
| tableView.deselectRow(at: indexPath, animated: true) | |
| sections[indexPath.section].rows[indexPath.row].action?() | |
| } | |
| override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| return sections[section].rows.count | |
| } | |
| func generateData() -> [TableViewSection] { return [] } | |
| } | |
| // Now static tableviews are simple | |
| class StaticTableViewExample: StaticTableViewBase { | |
| override func generateData() -> [TableViewSection] { | |
| let section1: TableViewSection = TableViewSection(title: "rewards", rows: { | |
| TableViewRow(type: BasicCell.self, | |
| cellConfiguration: { cell in | |
| // configure, cell is of type BasicCell | |
| }, | |
| action: { [weak self] in | |
| self?.performSegue(withIdentifier: "coolSegue", sender: nil) | |
| }), | |
| TableViewRow(type: FancyCell.self, | |
| cellConfiguration: { cell in | |
| // configure, cell is of type BasicCell | |
| }, | |
| action: { [weak self] in | |
| // do something | |
| }) | |
| ]) | |
| let section2: TableViewSection = TableViewSection(title: "rewards", rows: [ | |
| TableViewRow(type: BasicCell.self, | |
| cellConfiguration: { cell in | |
| // configure, cell is of type BasicCell | |
| }, | |
| action: { [weak self] in | |
| self?.performSegue(withIdentifier: "coolSegue", sender: nil) | |
| }), | |
| TableViewRow(type: FancyCell.self, | |
| cellConfiguration: { cell in | |
| // configure, cell is of type BasicCell | |
| }, | |
| action: { [weak self] in | |
| // do something | |
| }) | |
| ]) | |
| return [section1, section2] | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment