Inspired by Generic Table View Controllers by objc.io
- Reduces boilerplate code by implementing the most frequently used set of
UITableViewDelegate
andUITableViewDataSource
methods. - No need to change your models.
- Supports multiple cell types in the same
UITableView
. - Supports cells created in code or in a storyboard.
- Automatic cell registration for reuse.
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: 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...
}
}
- Option 1: Use
TableTie.SelectableRow
wrapper
...
tieAdapter.set([
SelectableRow(Song(title:"Paranoid"), self.playSong("Paranoid")),
SelectableRow(Song(title:"Iron Man"), self.playSong("Iron Man")),
])
...
- Option 2: Override
didSelectRow
for the row
extension Song: TableTie.Row {
...
func didSelectRow(of tableView: UITableView, at indexPath: IndexPath) {
// Implement your logic here...
}
...
}
- Option 3: Set
didSelect
closure forTableTie.Adapter
...
tieAdapter.didSelect = { row, tableView, indexPath in
// Implement your logic here...
}
...
TableTie
provides a quick and simple solution for displaying arbitrary data in UITableView
by implementing most frequently used set of UITableViewDataSource
and UITableViewDelegate
methods. For more advanced options, you can subclass TableTie.Adapter
and implement (or override) the relevant methods in your subclass.
TableTie.Adapter
implements the following methods:
numberOfSections(in:)
tableView(_:numberOfRowsInSection:)
tableView(_:cellForRowAt:)
tableView(_:titleForHeaderInSection:)
tableView(_:titleForFooterInSection:)
tableView(_:heightForRowAt:)
tableView(_:didSelectRowAt:)
You can install TableTie via CocoaPods by adding it to your Podfile
:
pod 'TableTie'
And run pod install
.
Add this to your Cartfile:
github "vladimirkofman/TableTie"
Then run carthage update
.
TableTie is available under the MIT license. See the LICENSE file for more info.