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.
// Create TableViewModel
self.tableViewModel = TableViewModel(tableView:self.tableView)
// Create and add the section
let tableSection = TableSection()
tableSection.headerTitle = "Section title"
tableSection.headerHeight = 30
self.tableViewModel.addSection(tableSection)
// 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
tableSection.addRow(tableRow)
tableRow.configureCell {
cell in
let label = cell.viewWithTag(1) as! UILabel
label.text = "Custom text"
}
tableRow.onSelect {
row in
NSLog("selected row \(row.userObject)")
}
// 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
tableSection.removeRow(tableRow)
// 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
tableSection.removeAllRows()
// Insert a section at an index
tableViewModel.insertSection(newSection, atIndex:0)
// Remove section
tableViewModel.removeSection(tableSection)
// Remove all sections
tableViewModel.removeAllSections()
tableRow.configureCell {
cell in
let myCustomCell = cell as! MyCustomCell
myCustomCell.setTitle("Custom title")
}
let customHeaderView = UIView() // Can be any UIView or subclass instance
tableSection.headerView = customHeaderView
tableSection.headerHeight = customHeaderView.frame.size.height
tableRow.height = Float(90)
tableRow.configureHeight {
return 100
}
tableSection.rowAnimation = UITableRowAnimation.Right
tableSection.addRow(newRow)
tableViewModel.sectionAnimation = UITableRowAnimation.Fade
tableViewModel.addSection(newSection)
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.