A data-driven library for building complex table views. Easy updating table view items with animations using automatic diffing algorithm under the hood. Our goal is to think in terms of data instead of index paths while building tables. High-level yet flexible api allows to setup sectioned lists in a few lines of code and take more control over the table where it’s needed. And reusable views configuring helps to keep code clean and stable in a type-safe manner.
- Animated table updates based on auto diffing
- Liner time diffing algorithm
- Type-safe cell, header/footer setup via protocol implementation
- No more
dequeReusable...
- No need to subclass either cell, table or model
- Cell initialization from xib, storyboard or code
- Simple yet flexible sections constructing
- Easy to extend
Item for cells must adopt Hashable
protocol.
Table view cell should conform to Configurable
protocol in order to receive cell item for setup.
extension Cell: Configurable {
public func setup(with item: User) {
textLabel?.text = item.name
}
}
Header/Footer view also should adopt Configurable
protocol to receive config item provided by Section
.
extension Header: Configurable {
public func setup(with item: String) {
textLabel?.text = item
}
}
Section Section<Item, SectionId>
is generic type and developer should provide cell models type (Item
) and section id type (SectionId
). It contains information about items, header/footer config (optionally) and must have unique id
.
HeaderFooterConfig
contains all information for section header/footer setup. There are two types of it: for default and custom header/footer view type.
let section = Section<User, Int>(
id: 0,
items: [...],
header: .default(title: "Section Begin")
)
let section = Section<User, Int>(
id: 0,
items: [...],
header: .custom(item: "Section Begin", reuseId: "FooterId")
)
Node: any type of item can be used for header/footer setup.
Create TableAdapter<Item, SectionId>
. Item and section id are associated types, like in Section
.
Then update adapter with sections.
class ViewController: UIViewController {
let tableView = ...
let users: [User] = [...]
private lazy var adapter = TableAdapter<User, Int>(
tableView: tableView,
cellIdentifierProvider: { (indexPath, item) -> String? in
return "Cell"
},
cellDidSelectHandler: { (table, indexPath, item) in
// Handle cell selection for item at indexPath
}
)
override func viewDidLoad() {
super.viewDidLoad()
setupTable()
let section = Section<User, Int>(
id: 0,
items: users,
header: .custom(item: "Begin", reuseId: "HeaderId"),
footer: .custom(item: "End", reuseId: "FooterId"),
)
adapter.update(with: [section], animated: true)
}
func setupTable() {
tableView.register(Cell.self, forCellReuseIdentifier: "Cell")
tableView.register(Header.self, forHeaderFooterViewReuseIdentifier identifier: "HeaderId")
tableView.register(Footer.self, forHeaderFooterViewReuseIdentifier identifier: "FooterId")
}
}
Note: this adapter type sets table view delegate to itself. To handle other table view delegate methods, you must inherit TableAdapter
and implement them.
You can also obtain current adapter sections unisng currentSections: [Section]
variable.
Sometimes you need to set delegate for cell, header or footer. For that purpose table adapter has sender
property, which will be passed to configurable view, that adopts SenderConfigurable
protocol.
extension Cell: SenderConfigurable {
func setup(with item: Item, sender: ViewController) {
textLabel?.text = object.name
delegate = sender
}
}
To use only one cell type, create adapter without CellReuserIdentifierProvider
lazy var adapter = TableAdapter<User, Int>(tableView: tableView)
and register cell via storyboard or code for default cell reuse identifier, which is "Cell" under the hood
tableView.register(Cell.self, forCellReuseIdentifier: adapter.defaultCellIdentifier)
- Swift 4.2+
- iOS 9.0+
Add the following to Podfile
:
pod 'TableAdapter'
Add the following to Cartfile
:
github "MobileUpLLC/TableAdapter"
Download and drag files from Source folder into your Xcode project.
TableAdapter is distributed under the MIT License.