在 SKScene 中使用 TableViewController [英] Use TableViewController inside SKScene

查看:18
本文介绍了在 SKScene 中使用 TableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我单击特定节点作为小窗口时,我都希望 TableViewController 出现在场景中.我创建了一个 TableViewController 类来配置它.这是我在 SkScene 中的代码:

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)

表类:

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
    }
}

问题是应该包含 tableview 的 UIView 出现了,但 table 本身没有.我会很感激任何帮助,或者即使有人有更好的方法来实现我想要的.

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.

推荐答案

您应该根据 frameView 的 bounds 设置您的 navViews/tableview 框架.

You should be setting your navViews/tableview frame based on the bounds of your frameView.

这将是 (0, 0, 200, 200).目前发生的情况是,当您的表被添加为框架视图的子视图时,它会偏移 100.因此它被绘制在框架视图边界之外.

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.

可能是这样的.

    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)

如果没有您的 Table 代码,很难知道还有什么地方可以使用它.

Hard to know where else to go with this without your Table code.

这篇关于在 SKScene 中使用 TableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆