Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a UITableView in Spritekit

I'm currently having an issue. I'm creating a game and I want to be able to use a UITableView to show data (Like levels). However, I'm using strictly SpriteKit and can't seem to get the UITableView and SpritKit to work.

I tried creating a variable in my 'GameScene' class (which is an SKScene) called 'gameTableView' and its value set to a class I made called 'GameRoomTableView'.

var gameTableView = GameRoomTableView()

The class had the value of 'UITableView' (notice that I did not set it to UITableViewController).

class GameRoomTableView: UITableView {
}

I was able to add the tableView as a subview of my SKView. I did this in my 'DidMoveToView' function that's inside my GameScene class. In which got the view to show.

self.scene?.view?.addSubview(gameRoomTableView)

However, I do not know how to change things like the number of sections and how to add cells.The class won't let me access those type of things unless it's a viewController and with that I'd need an actual ViewController to get it to work. I've seen many games use tableViews but I'm not sure how they got it to work, haha.

Please don't hesitate to tell me what I'm doing wrong and if you know of a better way of going about this. Let me know if you have any questions.

like image 886
Nick Griffith Avatar asked Jan 13 '17 02:01

Nick Griffith


1 Answers

Usually I don't prefer subclass the UITableView as you doing, I prefer to use the UITableView delegate and datasource directly to my SKScene class to control both table specs and data to my game code.

But probably you have your personal scheme so I make an example to you follow your request:

import SpriteKit
import UIKit
class GameRoomTableView: UITableView,UITableViewDelegate,UITableViewDataSource {
    var items: [String] = ["Player1", "Player2", "Player3"]
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        self.delegate = self
        self.dataSource = self
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - Table view data source
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
        cell.textLabel?.text = self.items[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.row)!")
    }
}
class GameScene: SKScene {
    var gameTableView = GameRoomTableView()
    private var label : SKLabelNode?
    override func didMove(to view: SKView) {
        self.label = self.childNode(withName: "//helloLabel") as? SKLabelNode
        if let label = self.label {
            label.alpha = 0.0
            label.run(SKAction.fadeIn(withDuration: 2.0))
        }
        // Table setup
        gameTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        gameTableView.frame=CGRect(x:20,y:50,width:280,height:200)
        self.scene?.view?.addSubview(gameTableView)
        gameTableView.reloadData()
    }
}

Output:

enter image description here

like image 146
Alessandro Ornano Avatar answered Oct 26 '22 13:10

Alessandro Ornano