TableNode
A TableView made of SpriteKit's Node.
How can you use it?
- You must import the
TableNode
to your SKScene's class.
import TableNode
- Create a variable of type
TableNode
, in the instance, you should set the frame and view. Later, set it'sDelegate
andDataSource
to self.
var tableNode: TableNode!
override func didMove(to view: SKView) {
self.tableNode = TableNode(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height), view: self.view!)
self.tableNode.delegate = self
self.tableNode.dataSource = self
self.addChild(self.tableNode)
}
- If you want, and I encourage you to do so. Set some position to your
TableNode
.
var tableNode: TableNode!
override func didMove(to view: SKView) {
self.tableNode = TableNode(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height), view: self.view!)
self.tableNode.delegate = self
self.tableNode.dataSource = self
self.tableNode.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5)
self.addChild(self.tableNode)
}
- Implement the
Delegate
's methods:
extension GameScene: TableNodeDelegate {
func tableNode(_ tableNode: TableNode, didSelectCell: TableNodeCell, at: IndexPath) {
print("\(String(describing: didSelectCell.name))")
}
}
- Implement the
DataSource
's methods:
extension GameScene: TableNodeDataSource {
func numberOfRows(inSection section: Int) -> Int {
return array.count
}
func tableNode(_ tableNode: TableNode, rowHeight indexPath: IndexPath) -> CGFloat {
return 100
}
func tableNode(_ tableNode: TableNode, cellForRowAt indexPath: IndexPath) -> TableNodeCell {
let cell = tableNode.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.addChild(nodes[indexPath.row])
return cell
}
}
- Everything is working.
๐
If you have any doubt about how TableNode works, you can get help here: Wiki
Disclaimer: The TableNode
has more methods than was shown here. And I write about then on Git's Wiki. They have default implementation, and works fine in this way. But, in the future, I expect to make the TableNode
more customable. ๐๐ฝ