问题
I want a TableViewController to appear inside the scene whenever I click on a specific node as a small window. I created a TableViewController class to configure it. Here is my code inside SkScene:
let table = Table()
let smallerRect = CGRectMake(100, 100, 200, 100)
let navRect = CGRectMake(0, 100, 200, 200)
let nav = UINavigationController(rootViewController: table)
nav.view.frame = navRect
let frameView = UIView(frame: smallerRect)
frameView.backgroundColor = UIColor.redColor()
table.view.frame = smallerRect
frameView.addSubview(nav.view)
self.view.addSubview(frameView)
Table class:
import UIKit
class Table: UITableViewController {
var names = ["name1", "name2", "name3"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = names[indexPath.row]
return cell
}
}
The problem is that the UIView that supposed to contain the tableview appears, but the table itself doesn't. I would appreciate any help or even if someone has a better way of achieving what I want.
回答1:
You should be setting your navViews/tableview frame based on the bounds of your frameView.
Which would be (0, 0, 200, 200). What's happening at the moment is that when your table gets added as a subview of your frame view, it is offset by 100. So it's drawn outside your frame views bounds.
Something like this maybe.
let table = Table()
let smallerRect = CGRectMake(100, 100, 200, 100)
let navRect = CGRectMake(0, 0, 200, 200)
let nav = UINavigationController(rootViewController: table)
nav.view.frame = navRect
let frameView = UIView(frame: smallerRect)
frameView.backgroundColor = UIColor.redColor()
table.view.frame = frameView.bounds
frameView.addSubview(nav.view)
self.view.addSubview(frameView)
Hard to know where else to go with this without your Table
code.
来源:https://stackoverflow.com/questions/37557933/use-tableviewcontroller-inside-skscene