- July 9, 2025
- Mins Read
cellmodel-driven collectionview manager
UICollectionViewDelegate
and UICollectionViewDataSource
protocols
// viewController swift file
let sapporo = Sapporo(collectionView: self.collectionView)
let cellmodel = YourCellModel(title: “Title”, des: “description”) {
println(“Did select cell with title = \(title)”)
}
let topSection = SASection()
sapporo
.reset(topSection)
.bump()
topSection
.append(cellmodel)// append a new cell model in datasource
.bump()// show the new cell in the collection view
topSection
.remove(1…3)
.bump()
topSection
.move(fromIndex: 0, toIndex: 3)
.bump()
// your cell swift file
class YourCellModel : SACellModel {
let title: String
let des: String
init(title: String, des: String, selectionHandler: (SACell) -> Void) {
self.title = title
self.des = des
super.init(cellType: YourCell.self, selectionHandler: selectionHandler)
}
}
class YourCell : SACell, SACellType {
typealias CellModel = YourCellModel
@IBOutlet weak var titleLabel: UILabel!
override func configure() {
super.configure()
guard let cellmodel = cellmodel else {
return
}
titleLabel.text = cellmodel.title
}
}
}
}
// inserting
sapporo.insert(section, atIndex: 1)
.bump()
// moving
sapporo.move(fromIndex: 1, toIndex: 5)
.bump()
// removing
sapporo.remove(index)
.bump()
// remove all data
sapporo.reset()
.bump()
// handing section index by enum
enum Section: Int, SASectionIndexType {
case top
case center
case bottom
static let count = 3
}
let topSection = sapporo[Section.Top]
// appending
sapporo[0]
.append(cellmodel)// append a cellmodel
.bump()// and bump to show the cell in the collection view
sapporo[TopSection]
.append(cellmodels)// append a list of cellmodels
.bump()
// by using section
let section = sapporo[Section.Top]
section
.append(cellmodel)
.bump()
// 2. inserting
section
.insert(cellmodels, atIndex: 1)
.bump()
section
.insertBeforeLast(cellmodels)
.bump()
// 3. reseting
section
.reset(cellmodels)// replace current data in section by the new data
.bump()
section
.reset()// or remove all data in section
.bump()
// 4. moving
section
.move(fromIndex: 5, toIndex: 1)
.bump()
// 5. removing
section
.remove(1)
.bump()
section
.remove(cellmodel)
.bump()
section
.remove(2…5)
.bump()
section
.removeLast()
.bump()
// updating cell
let cellmodel = section[1]
cellmodel.property = newData
cellmodel.bump()
// able to retrieve a cellmodel by indexpath
let cellmodel = sapporo[indexpath]
sapporo
.registerCellByNib(CustomCell)
.registerCell(SimpleCell)
.registerSupplementaryViewByNib(HeaderView.self, kind: “SectionHeader”)
In case you want to customize the layout of collection view, just create a subclass of SALayout and call setLayout
method to set the new layout instance.
class CustomLayout: SALayout {
// the implementation for your layout
}
let layout = CustomLayout()
sapporo.setLayout(layout)
Using Carthage
nghialv/Sapporo
to your Cartfilecarthage update
Using CocoaPods
use_frameworks!
target ‘YOUR_TARGET_NAME’ do
pod ‘Sapporo’
end
pod install
Using submodule
A lightweight sparkline component, supporting Swift, SwiftUI, macCatalyst and Objective-C. What is a sparkline? A sparkline is a very ...
LightChart is a lightweight SwiftUI package with line charts implementation. You can use it when you need only a chart that will perfectly ...
Swift package for displaying charts effortlessly. V2 Beta is here 🎉🎉🎉 V2 focuses on providing a strong and easy to ...
Lightweight and easy to use SwiftUI chart library for all Apple platforms