Created
September 12, 2016 14:27
-
-
Save lucasp90/cdac1a3c9c7bbde07d80f35dbeaa4858 to your computer and use it in GitHub Desktop.
TableViewController configuration example
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
struct PokemonTableViewConfiguration { | |
var items: [Pokemon] | |
var configuration: TableViewConfiguration<Pokemon, PokemonTableViewCell> { | |
return TableViewConfiguration<Pokemon, PokemonTableViewCell>(title: "Pokemons", items: items, style: .Plain, estimatedRowHeight: 160.0, cellSpacingStyle: .WithSpacing(size: 8), selectableRows: false) { (cell, item) -> Void in | |
cell.lblName = item.name | |
cell.lblHitPoints = item.hitPoints | |
let btnCatch = UIButton(frame: CGRectZero) | |
btnCatch.addTarget(nil, action: #selector(<something_here>), forControlEvents: .UITouchUpInside) | |
} | |
} | |
} |
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
import UIKit | |
public enum TableViewCellSpacingStyle { | |
case WithSpacing(size: CGFloat) | |
case WithoutSpacing | |
} | |
struct TableViewConfiguration<Item, Cell> { | |
var title: String? | |
var items: [Item] | |
var style: UITableViewStyle | |
var estimatedRowHeight: CGFloat | |
var cellSpacingStyle: TableViewCellSpacingStyle | |
var selectableRows: Bool | |
var configureCell: (cell: Cell, item: Item) -> Void | |
} | |
class TableViewController<Item, Cell: UITableViewCell where Cell: ReusableView>: UITableViewController { | |
let items: [Item] | |
let estimatedRowHeight: CGFloat | |
let configureCell: (cell: Cell, item: Item) -> Void | |
let selectableRows: Bool | |
let cellSpacingStyle: TableViewCellSpacingStyle | |
init(configuration: TableViewConfiguration<Item, Cell>) { | |
self.items = configuration.items | |
self.estimatedRowHeight = configuration.estimatedRowHeight | |
self.configureCell = configuration.configureCell | |
self.cellSpacingStyle = configuration.cellSpacingStyle | |
self.selectableRows = configuration.selectableRows | |
super.init(style: configuration.style) | |
self.title = configuration.title | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
self.tableView.registerClass(Cell.self, forCellReuseIdentifier: Cell.reuseIdentifier) | |
} | |
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
switch self.cellSpacingStyle { | |
case .WithoutSpacing: | |
return 1 | |
case .WithSpacing(_): | |
return items.count | |
} | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
switch self.cellSpacingStyle { | |
case .WithoutSpacing: | |
return items.count | |
case .WithSpacing(_): | |
return 1 | |
} | |
} | |
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { | |
return self.estimatedRowHeight | |
} | |
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { | |
return UITableViewAutomaticDimension | |
} | |
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { | |
cell.userInteractionEnabled = self.selectableRows | |
} | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(indexPath: indexPath) as Cell | |
let item = self.items[indexPath.row] | |
configureCell(cell: cell, item: item) | |
return cell | |
} | |
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
return UIView(frame: CGRectZero) | |
} | |
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
switch self.cellSpacingStyle { | |
case .WithoutSpacing: | |
return 0 | |
case .WithSpacing(let size): | |
return size | |
} | |
} | |
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { | |
return UIView(frame: CGRectZero) | |
} | |
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { | |
switch self.cellSpacingStyle { | |
case .WithoutSpacing: | |
return 0 | |
case .WithSpacing(let size): | |
return size | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment