TableViewModel 0.1.3

TableViewModel 0.1.3

LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2016
SPMSupports SPM

Maintained by Tunca Bergmen.

  • By
  • tbergmen


TableViewModel lets you create your UITableView instances declaratively in Swift. You can add, insert or remove sections and rows without worrying about implementing UITableViewDelegate and UITableViewDataSource methods.

TableViewModel registers different types of cells as reusable cells in the TableView, and uses dequeueReusableCellWithIdentifier when it needs them. From a performance point of view it is not any different than manually implementing UITableViewDataSource.

TableViewModel is inspired from DXTableViewModel but it is not a Swift implementation of DXTableViewModel. It is written from the ground up in Swift and does many things differently.


To run the example project, clone the repo, and run pod install from the Example directory first.


Adding sections and rows:

// Create TableViewModel
self.tableViewModel = TableViewModel(tableView:self.tableView)

// Create and add the section
let tableSection = TableSection()
tableSection.headerTitle = "Section title"
tableSection.headerHeight = 30

// Create and add the row
let tableRow = TableRow(cellIdentifier:"MyCell") // Cell identifier is either the reuse identifier of a reusable cell, or name of an XIB file that contains one and only one UITableViewCell object
tableRow.userObject = "My tag" // Optional <AnyObject> property to identify the row later

Configuring the rows

tableRow.configureCell {
    cell in
    let label = cell.viewWithTag(1) as! UILabel
    label.text = "Custom text"

Selection handler

tableRow.onSelect {
    row in
    NSLog("selected row \(row.userObject)")

Adding, inserting and removing rows

// Adding multiple rows
tableSection.addRows(arrayOfRows) // IMPORTANT: Notice that this performs much faster than inserting a bunch of rows one by one in a loop
// Insert a row at an index
tableSection.insertRow(newRow, atIndex:0)
// Remove a row
// Remove multiple rows
tableSection.removeRows(arrayOfRows) // IMPORTANT: Notice that this performs much faster than removing a bunch of rows one by one in a loop
// Removing all rows

Inserting removing sections

// Insert a section at an index
tableViewModel.insertSection(newSection, atIndex:0)
// Remove section
// Remove all sections

Cell with custom sub class

tableRow.configureCell {
    cell in
    let myCustomCell = cell as! MyCustomCell
    myCustomCell.setTitle("Custom title")

Custom section header views

let customHeaderView = UIView() // Can be any UIView or subclass instance
tableSection.headerView = customHeaderView
tableSection.headerHeight = customHeaderView.frame.size.height

Custom row height

tableRow.height = Float(90)

Custom row height with closure

tableRow.configureHeight {
    return 100

Row animation for adding, inserting and removing rows

tableSection.rowAnimation = UITableRowAnimation.Right

Section animation for adding, inserting and removing sections

tableViewModel.sectionAnimation = UITableRowAnimation.Fade


TableViewModel is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "TableViewModel"


Tunca Bergmen, [email protected]


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