如何在Xcode Playgrounds上填充桌面

发布于 2025-01-30 16:27:03 字数 1712 浏览 4 评论 0原文

我想在 Xcode Playgrounds 上创建一个具有TableView的应用程序,该tableView从XML文件中获取数据。我该怎么做?

我的XML是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/ PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <array>
            <string>María Dolores Pilar</string>
            <string>11.327</string>
            <string>9.274</string>
            <string>7.554</string>
        </array>
    </array>
</plist>

应该将其保存在一个结构的人中:

struct Person {
    var name:String
    var stones:Double
    var pounds:Double
    var ounces:Double
    var kilograms:Double {
        get {
            stones*6.35+pounds*0.4536+ounces*0.02835
        }
    }
}

然后用这些信息填充表观视图,

class MyViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var list = [Person]()
    var selectedRowNumber:Int?
    
    var tableView:UITableView!

    var myConstraints:[NSLayoutConstraint]!

    func addComponents() -> Void {
        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints=false

       self.view.addSubview(tableView)
    }

    override func loadView() {
        super.loadView()
        
        self.view.frame = CGRect(x: 0, y: 0, width: 512, height: 768)
        
        self.addComponents()
        self.setUpConstraints()
        
        NSLayoutConstraint.activate(myConstraints)
    }
}

PlaygroundPage.current.liveView = MyViewController()

如何在操场上编程地填充表观视图?

先感谢您。

I want to create an app on Xcode playgrounds that has a tableView which gets the data from a XML file. How can I do this?

My XML is like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/ PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <array>
            <string>María Dolores Pilar</string>
            <string>11.327</string>
            <string>9.274</string>
            <string>7.554</string>
        </array>
    </array>
</plist>

It should be saved in a struct Person:

struct Person {
    var name:String
    var stones:Double
    var pounds:Double
    var ounces:Double
    var kilograms:Double {
        get {
            stones*6.35+pounds*0.4536+ounces*0.02835
        }
    }
}

And then populate the TableView with that information

class MyViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var list = [Person]()
    var selectedRowNumber:Int?
    
    var tableView:UITableView!

    var myConstraints:[NSLayoutConstraint]!

    func addComponents() -> Void {
        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints=false

       self.view.addSubview(tableView)
    }

    override func loadView() {
        super.loadView()
        
        self.view.frame = CGRect(x: 0, y: 0, width: 512, height: 768)
        
        self.addComponents()
        self.setUpConstraints()
        
        NSLayoutConstraint.activate(myConstraints)
    }
}

PlaygroundPage.current.liveView = MyViewController()

How can I populate the TableView programmatically in playgrounds?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文