Today we'll check out a library called Shoyu that aims to make setting up UITableViews a breeze. Let's dive in:
Shoyu works as a sort of DSL. It adds a source property on to UITableView, then gives as a few functions like createSection
and createRow
to fill the table view with content. Let's see what it takes to add a section and a row:
tableView.source = Source() { source in
source.createSection { section in
section.createRow { (row: Row<SpaceshipTableViewCell>) in
row.reuseIdentifier = "SpaceshipCell"
row.height = 52
row.configureCell = { cell, _ in cell.nameLabel.text = "Tantive IV" }
}
}
}
We can use a similar technique to create and attach headers and footers to sections:
section.createHeader {
$0.reuseIdentifier = "SpaceshipHeader"
$0.height = 120
$0.configureView = { view, _ in
view.backgroundColor = .blackColor()
}
}
There's also a few other functions to help us do things like dynamically set a row's height, or run some code when a row is selected:
section.createRow { row in
row.didSelect = { _ in /**/ }
row.heightFor = { indexPath -> CGFloat? in
return calculateHeight(indexPath)
}
}
Last but not least, we can pass in an array of our model objects, then easily configure our cells with them:
section.createRows(spaceships) { (spaceship, row: Row<SpaceshipTableViewCell>) in
row.height = 52
row.configureCell = { cell, _ in cell.configure(spaceship) }
}
More info about Shoyu can be found at git.io/shoyu