TableNode
由 SpriteKit 的 Node 组成的 TableView。
您如何使用它?
- 您必须在 SKScene 的类中导入
TableNode
。
import TableNode
- 创建一个类型为
TableNode
的变量,在实例中应设置框架和视图。然后,将其Delegate
和DataSource
设置为 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)
}
- 如果您愿意,我强烈建议这样做。为您的
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)
}
- 实现
Delegate
的方法
extension GameScene: TableNodeDelegate {
func tableNode(_ tableNode: TableNode, didSelectCell: TableNodeCell, at: IndexPath) {
print("\(String(describing: didSelectCell.name))")
}
}
- 实现
DataSource
的方法
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
}
}
- 一切正常。
🎉
如果您对 TableNode 的工作原理有任何疑问,您在这里可以获得帮助:[Wiki](https://github.com/ramoliveira/TableNode/wiki)
免责声明: TableNode
的方法比这里展示的要多。我在 Git 的 Wiki 上写了关于它们的内容。它们有默认实现,并以这种方式正常运行。但是,在未来,我期望使 TableNode
更加可自定义。👍🏽