@@ -44,8 +44,9 @@ class CellLogs: UICollectionViewCell, SlotableCell {
4444
4545 static let slotWidth = 1 // size of cell in grid 📏
4646 static let slotHeight = 1 // size of cell in grid 📐
47+ var slotParams: [String : Any ] = [: ]
4748
48- func load (params : [ String : Any ] ) {
49+ func load () {
4950 // this method if called when a cell is created in grid 🔨
5051 }
5152}
@@ -69,7 +70,7 @@ extension ViewController: GridViewDelegate {
6970 // if do you want list all classes that subscreber the SlotableCell protocol, you can read use this gist: https://gist.github.com/brunomacabeusbr/eea343bb9119b96eed3393e41dcda0c9 💜
7071 }
7172
72- func setup (cell : UICollectionViewCell, params : [ String : Any ] ) {
73+ func setup (cell : SlotableCell ) {
7374 // this delegate is called in "collectionView(_:cellForItemAt)" from GridViewController
7475 // it's useful when we need to setup many cells with same code 🍡
7576
@@ -79,7 +80,7 @@ extension ViewController: GridViewDelegate {
7980 }
8081
8182 // layout
82- cell.layer .cornerRadius = 10
83+ ( cell as? UICollectionViewCell) ? .layer .cornerRadius = 10
8384 }
8485}
8586```
@@ -98,11 +99,12 @@ class ViewController: UIViewController {
9899 super .viewDidLoad ()
99100
100101 // set the cells to show in grid 📌
101- containerGrid! .gridConfiguration = [
102+ containerGrid! .gridConfiguration = GridConfiguration. create ( slots : Slots ( slots : [
102103 [Slot (cell : CellMap.self , params : [: ]), Slot (cell : CellChart.self , params : [: ])],
103104 [Slot (cell : CellLogs.self , params : [: ])],
104105 [Slot (cell : CellCharacter.self , params : [" race" : " troll" ]), Slot (cell : CellCharacter.self , params : [" race" : " elves" ]), Slot (cell : CellCharacter.self , params : [" race" : " undead" ]), Slot (cell : CellCharacter.self , params : [" race" : " merfolk" ])]
105- ]
106+ ])
107+ )
106108 }
107109
108110 override func prepare (for segue : UIStoryboardSegue, sender : Any ? ) {
@@ -125,25 +127,29 @@ When you create a `Slot`, you set a cell of this slot, and also **params** of th
125127Slot (cell : CellCharacter.self , params : [" race" : " undead" ])
126128```
127129
128- The parameter is passed in two moments:
129- * on ` GridViewDelegate ` , in method ` setup(cell: UICollectionViewCell, params: [String: Any]) `
130- * on ` SlotableCell ` , in method ` load(params: [String: Any]) `
130+ The value of ` params ` is set in attribute ` slotParams ` of ` SlotableCell ` .
131131
132132Example of use:
133133
134134``` swift
135- func load (params : [String : Any ]) {
136- let paramRace = params[" race" ] as? String
135+ class CellCharacter : UICollectionViewCell , SlotableCell {
137136
138- switch paramRace { // imagens from the amazing open source game Battle for Wesnoth
139- case " undead" ? :
140- image.image = UIImage (named : " undead" )!
141- case " elves" ? :
142- image.image = UIImage (named : " elves" )!
143- case " troll" ? :
144- image.image = UIImage (named : " troll" )!
145- default :
146- print (" invalid race: \( paramRace ?? " nil" ) " )
137+ ...
138+ var slotParams: [String : Any ] = [: ]
139+
140+ func load () {
141+ let paramRace = slotParams[" race" ] as? String
142+
143+ switch paramRace {
144+ case " undead" ? :
145+ image.image = UIImage (named : " undead" )!
146+ case " elves" ? :
147+ image.image = UIImage (named : " elves" )!
148+ case " troll" ? :
149+ image.image = UIImage (named : " troll" )!
150+ default :
151+ print (" invalid race: \( paramRace ?? " nil" ) " )
152+ }
147153 }
148154}
149155```
0 commit comments