CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.
TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Apr 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Vladimir Kofman.
UITableView
with data.Inspired by Generic Table View Controllers by objc.io
Let’s say you have a list of Songs and Albums that you want to display in UITableView
. Your models can be structs, classes, enums, or just Strings. Let’s use structs for the example:
struct Song {
let title: String
}
struct Album {
let name: String
let artist: String
}
class SongCell: UITableViewCell {}
class AlbumCell: UITableViewCell {}
You’ll need your models to conform to TableTie.Row protocol. There’s only one required method that you’ll need to implement: func configure(cell:)
. In this method you’ll configure your cell according to your model. Make sure to use the correct type for the cell:
parameter: it’s generic, and will work for any subclass of UITableViewCell
.
extension Song: TableTie.Row {
func configure(cell: SongCell) {
cell.textLabel?.text = title
}
}
extension Album: TableTie.Row {
func configure(cell: AlbumCell) {
cell.textLabel?.text = "\(name) by \(artist)"
}
}
In your view controller, you’ll need to create an instance of TableTie.Adapter
, and provide it with the array of your models.
class MyVC: UITableViewController {
let tieAdapter =
TableTie.Adapter([
Album(name: "Paranoid", artist:"Black Sabbath"),
Song(title: "War Pigs"),
Song(title: "Paranoid"),
Song(title: "Planet Caravan")])
override func viewDidLoad() {
tableView.delegate = tieAdapter
tableView.dataSource = tieAdapter
super.viewDidLoad()
}
}
Don’t forget to set .delegate and .dataSource for the tableView to the TableTie.Adapter
instance you’ve created earlier.
Please read on for more options…
...
let tieAdapter = TableTie.Adapter([
TableTie.Section("Album", [
Album(name: "Paranoid", artist:"Black Sabbath"),
]),
TableTie.Section("Side one", [
Song(title: "War Pigs"),
Song(title: "Paranoid"),
Song(title: "Planet Caravan"),
Song(title: "Iron Man"),
])])
...
TableTie
will automagically register your cell for UITableViewCell
reuse. If you need to use a specific reuseIdentifier
, or if you’ve designed your cell in a storyboard, just override the reuseIdentifier
property of TableTie.Row
:
extension Song: TableTie.Row {
var reuseIdentifier: String { return "reuseMePlease" } // <-- HERE
func configure(cell: YourStoryboardCell) {
//Do what you have to do here...
}
}
extension Song: TableTie.Row {
var reuseIdentifier: String { return "reuseMePlease" }
var rowHeight: CGFloat { return 100.0 } // <-- HERE
func configure(cell: YourStoryboardCell) {
//Do what you have to do here...
}
}
TableTie.SelectableRow
wrapper...
tieAdapter.set([
SelectableRow(Song(title:"Paranoid"), self.playSong("Paranoid")),
SelectableRow(Song(title:"Iron Man"), self.playSong("Iron Man")),
])
...
didSelectRow
for the rowextension Song: TableTie.Row {
...
func didSelectRow(of tableView: UITableView, at indexPath: IndexPath) {
// Implement your logic here...
}
...
}
didSelect
closure for TableTie.Adapter
...
tieAdapter.didSelect = { row, tableView, indexPath in
// Implement your logic here...
}
...
TableTie is available under the MIT license. See the LICENSE file for more info.