StructureKit is an simplest way to control very very hard tables or collections.
Features
๐ Forget exceptions likeattempt to insert, delete, move etc...
๐ ForgetnumberOfRows, numberOfItems, cellForRow etc...
๐ ForgetinsertRows, deleteRows etc...
๐ Most simplest building table or collection views๐คฉ Automatic diff calculation for animations (insert, delete, move, update)๐ Super easy creating and controlling collections with different cell types๐คจ Does not store in memory previously used models for diff calculation๐ค Using onecellModel
for bothtableView
andcollectionView
Getting Started
Installation
pod 'StructureKit'
Model
Make some models
// City will be used as cell
struct City {
let title: String
}
// Country will be used as section
struct Country {
let title: String
let cities: [City]
}
Declare City
model for using in tableView
.
I recommend to create cell viewModel, but for simple demonstration, we use directly data entity.
Implement StructurableIdentifable
for identify model in diffing calculation (insert
, delete
, move
)
extension City: StructurableIdentifable {
func identifyHash(into hasher: inout Hasher) {
hasher.combine(name)
}
}
Implement StructurableForTableView
for using in tableView
and configure cell with model data.
extension City: StructurableForTableView {
// Reuse identifier will be registered as class name
func configure(tableViewCell cell: CityTableViewCell) {
cell.textLabel?.text = name
}
}
ViewController
Implementing viewController
with tableView
class TableViewController: UIViewController {
var countries: [Country] = []
@IBOutlet var tableView: UITableView!
let structureController = StructureController()
override func viewDidLoad() {
super.viewDidLoad()
loadCountries()
configureTableView()
makeStructure()
}
func loadCountries() {
// some data loading
self.countries = [
Country(title: "USA", cities: [City(title: "New York"), City(title: "Los Angeles"), City(title: "Cupertino")]),
Country(title: "Russia", cities: [City(title: "Moscow"), City(title: "Rostov-on-Don"), City(title: "Yeysk")])
]
}
// Register tableView and cell types in StructureController
func configureTableView() {
// You can make registration once per instance
structureController.register(tableView, cellModelTypes: [
City.self
])
}
// Create table structure
func makeStructure() {
let structure = countries.map { country in
var section = StructureSection(
identifier: country.title,
rows: country.cities
)
section.header = .text(country.name)
return section
}
structureController.set(structure: structure)
}
}
Done!