Remove row builder |
||
|---|---|---|
| Configs | ||
| Demo | ||
| Sources | ||
| TableKit.xcodeproj | ||
| Tests | ||
| .gitignore | ||
| .travis.yml | ||
| LICENSE | ||
| Package.swift | ||
| README.md | ||
| TableKit.podspec | ||
README.md
#TableKit
TableKit is a super lightweight yet powerful generic library that allows you to build complex table views in a declarative type-safe manner.
It hides a complexity of UITableViewDataSource and UITableViewDelegate methods behind the scene, so your code will be look clean, easy to read and nice to maintain.
Features
- Type-safe generic cells
- Functional programming style friendly
- The easiest way to map your models or view models to cells
- Correctly handles autolayout cells with multiline labels
- Chainable cell actions (select/deselect etc.)
- Support cells created from code, xib, or storyboard
- Support different cells height calculation strategies
- Support portrait and landscape orientations
- No need to subclass
- Extensibility
Getting Started
An example app is included demonstrating TableKit's functionality.
Basic usage
Create your rows:
let row1 = TableRow<String, StringTableViewCell>(item: "1")
let row2 = TableRow<Int, IntTableViewCell>(item: 2)
let row3 = TableRow<Float, FloatTableViewCell>(item: 3.0)
Put rows into section:
let section = TableSection(rows: [row1, row2, row3])
And setup your table:
let tableDirector = TableDirector(tableView: tableView)
tableDirector.register(StringTableViewCell.self, IntTableViewCell.self, FloatTableViewCell.self)
tableDirector += section
Done. Your table is ready. You may want to look at your cell. It has to conform to ConfigurableCell protocol:
class StringTableViewCell: UITableViewCell, ConfigurableCell {
typealias T = String
func configure(string: T, isPrototype: Bool) {
titleLabel.text = string
}
static func estimatedHeight() -> CGFloat {
return 44
}
}
You could have as many rows and sections as you need.
Row actions
It nice to have some actions that related to your cells:
let action = TableRowAction<String, StringTableViewCell>(.click) { (data) in
// you could access any useful information that relates to the action
// data.cell - StringTableViewCell?
// data.item - String
// data.path - NSIndexPath
}
let row = TableRow<String, StringTableViewCell>(item: "some", actions: [action])
Or, using nice chaining approach:
let row = TableRow<String, StringTableViewCell>(item: "some")
.action(TableRowAction(.click) { (data) in
})
.action(TableRowAction(.shouldHighlight) { (data) -> Bool in
return false
})
You could find all available actions here.
Advanced
Cell height calculating strategy
By default TableKit relies on self-sizing cells. In that case you have to provide an estimated height for your cells:
class StringTableViewCell: UITableViewCell, ConfigurableCell {
// ...
static func estimatedHeight() -> CGFloat {
return 44
}
}
It's enough for most cases. But you may be not happy with this. So you could use a prototype cell to calculate cell's heights. To enable this feature simply use this property:
tableDirector.shouldUsePrototypeCellHeightCalculation = true
It does all dirty work with prototypes for you behind the scene, so you don't have to worry about anything except of your cell configuration:
class ImageTableViewCell: UITableViewCell, ConfigurableCell {
func configure(url: NSURL, isPrototype: Bool) {
if !isPrototype {
loadImageAsync(url: url, imageView: imageView)
}
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutIfNeeded()
multilineLabel.preferredMaxLayoutWidth = multilineLabel.bounds.size.width
}
}
First of all you have to set preferredMaxLayoutWidth for all your multiline labels. And check if a configuring cell is a prototype cell. If it is, you don't have to do any additional work that not actually affect cell's height. For example you don't have to load remote image for a prototype cell.
Functional programming
It's never been so easy to deal with table views.
let users = /* some users array */
let rows: [Row] = users.filter({ $0.state == .active }).map({ TableRow<String, UserTableViewCell>(item: $0.username) })
tableDirector += rows
Done, your table is ready. It's just awesome!
Installation
CocoaPods
To integrate TableKit into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'TableKit'
Carthage
Add the line github "maxsokolov/tablekit" to your Cartfile.
Manual
Clone the repo and drag files from Sources folder into your Xcode project.
Requirements
- iOS 8.0+
- Xcode 7.0+
License
TableKit is available under the MIT license. See LICENSE for details.