vladimirkofman / TableTie

A quick, generic way to populate a UITableView with data.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TableTie

Build Status CocoaPods Compatible Carthage Compatible Code coverage status CocoaPods Platform support License MIT

A quick, generic way to populate a UITableView with data.

Inspired by Generic Table View Controllers by objc.io

Features

  • Reduces boilerplate code by implementing the most frequently used set of UITableViewDelegate and UITableViewDataSource 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.

Usage

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.

And that's all!

Please read on for more options...

Sections

...
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"),
        ])])
...

Custom reuseIdentifier and Storyboard support

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...
    }
}

Row height

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...
    }
}

Row selection

  • 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 for TableTie.Adapter
...
tieAdapter.didSelect = { row, tableView, indexPath in
    // Implement your logic here...
}
...

More options

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:

UITableViewDataSource

  • numberOfSections(in:)
  • tableView(_:numberOfRowsInSection:)
  • tableView(_:cellForRowAt:)
  • tableView(_:titleForHeaderInSection:)
  • tableView(_:titleForFooterInSection:)

UITableViewDelegate

  • tableView(_:heightForRowAt:)
  • tableView(_:didSelectRowAt:)

Installation

CocoaPods

You can install TableTie via CocoaPods by adding it to your Podfile:

pod 'TableTie'

And run pod install.

Carthage

Add this to your Cartfile:

github "vladimirkofman/TableTie"

Then run carthage update.

License

TableTie is available under the MIT license. See the LICENSE file for more info.

About

A quick, generic way to populate a UITableView with data.

License:MIT License


Languages

Language:Swift 95.7%Language:Ruby 4.3%