Shoyu is a library written in Swift to represent UITableView data structures.
Shoyu means Soy Source in Japanese.
Use createSection
and createRow
.
tableView.source = Source() { source in
// Create section
source.createSection { section in
// Create row
section.createRow { row in
// Setting reuse identifier
row.reuseIdentifier = "Cell"
// Setting fixed height.
row.height = 52
// Configuring handler for cell.
row.configureCell = { cell, _ in
cell.textLabel?.text = "row 1"
}
}
}
}
if ClassName
and ReuseIdentifier
specified in Storyboard are the same, you don't need to specify the reuseIdentifier.
Use createRows
.
let members = [
Member(firstName: "John", lastName: "Paterson"),
Member(firstName: "Matt", lastName: "Gray"),
Member(firstName: "Jennifer", lastName: "Hart"),
Member(firstName: "Katherine", lastName: "Nash"),
Member(firstName: "Diane", lastName: "Nash"),
]
tableView.source = Source() { source in
source.createSection { section in
section.createRows(members) { member, row in
row.height = 52
row.configureCell = { cell, _ in
cell.textLabel?.text = member.fullName
}
}
}
}
Use createHeader
and createFooter
.
tableView.source = Source() { source in
source.createSection { section in
// Create header.
section.createHeader { header in
// Setting title.
header.title = "Header"
header.height = 22
header.configureView = { view, _ in
view.backgroundColor = UIColor.lightGrayColor()
}
}
// Create footer.
section.createFooter { footer in
...
}
}
}
Section
and Row
is compatible with generics.
public class Section<HeaderType: UIView, FooterType: UIView>: SectionType {
...
}
public class Row<CellType: UITableViewCell>: RowType {
...
}
cell
is an argument of configureCell
is the type specified in the generics.
section header and section footer are also similar.
// Create generic row.
section.createRows(members) { (member, row: Row<MemberTableViewCell>) in
row.configureCell = { cell, _ in
// cell type is MemberTableViewCell.
cell.nameLabel.text = member.fullName
}
}
Row
has some delegate methods. configureCell
, heightFor
, didSelect
and so on.
section.createRow { row in
// Configuring handler for height.
row.heightFor = { _ -> CGFloat? in
return 52
}
// Configuring handler for cell.
row.configureCell = { cell, _ in
cell.textLabel?.text = "row"
}
// Event handler for when cell is selected.
row.didSelect = { _ in
print("row is selected.")
}
}
Shoyu is released under the MIT license. See LICENSE for details.