将数据解析为 NSTableView [英] Parse Data into NSTableView

查看:49
本文介绍了将数据解析为 NSTableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位程序员好,

首先,我是 OSX/iOS 编程的新手,正在尝试快速学习.我在 Java 方面相当能干,想自学一些新东西.我知道 swift 编程语言还处于早期阶段.

First off, I am new to OSX/iOS programming and am trying to learn swift. I am fairly competent in java and wanted to teach myself something new. I am aware that the swift programming language is in it's early stages.

我有一个使用 parse.com 数据浏览器的表格.我可以从该表中获取元素并将它们存储在一个数组中.通常,在 Java 中,我会简单地遍历这些元素并填充 JTable.现在,我要问你的问题是……我将如何做这件事?我试图阅读苹果 api,我觉得我的智商下降了.我可以理解 getNumberOfRows 函数/方法,但其他函数对我来说毫无意义.我的 xib 文件中有一个 NSTable,并且将委托和数据源放在应用程序委托文件中.如何设置标识符以便我可以开始设置信息.

I have a table using the parse.com data browser. I can get the elements from that table and store them in an array. Normally, in java, I would then simply iterate over those elements and populate a JTable. Now, my question to you is...How would I go about doing this? I have tried to read the apple api and I felt my IQ dropping. I can understand the getNumberOfRows function/method however the other functions mean nothing to me. I have an NSTable in my xib file and have west the delegate and datasource to the app delegate file. How do I set the identifier so that I can start setting information.

我知道我并没有真正提供帮助,但非常感谢任何建议.

I understand that I am not really helping but any advice is greatly appreciated.

谢谢.

推荐答案

是的,使用 Cocoa 的 tableviews 是......很复杂.

Yes, tableviews with Cocoa are... complicated.

tableview中获取内容的绝对基础是设置它的数据源,必须采用NSTableViewDataSource协议.然后你必须区分基于视图和基于单元格的表格视图.在基于视图中,您可以使用在 Interface Builder 中设计的视图,基于单元格的更旧且更简单.您了解 numberOfRowsInTableView 函数,所以我继续执行更复杂的函数.

The absolute basics for getting content in the tableview is to set its datasource, which has to adopt NSTableViewDataSource protocol. Then you have to differentiate between a view based and a cell based tableview. In the view based you can use a view you designed in Interface Builder, the cell based are older and simpler. You understand the numberOfRowsInTableView function, so I proceed with the more complicated functions.

对于基于单元格的 tableViews,第二个基本函数## Heading ## 是

For cell based tableViews, the second essential function## Heading ## is

func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!

当你刚拿到它时,这很容易.作为第一个参数,您将获得 tableView(仅当您对多个 tableView 使用相同的数据源时才有趣).第二个指定 tableColumn.您可以使用其标识符来标识列.您可以在 InterfaceBuilder 中设置此标识符.单击您的 tableview,直到选择一列.然后在侧边栏中设置恢复 ID.作为最后一个参数,您将获得您的行.您返回一个 AnyObject 类型的对象.我一般返回一个字符串,不知道NSNumber是否也有效.一个简单的示例实现如下:

When you just got it, it's pretty easy. As first parameter you get the tableView (only interesting if you use the same dataSource for multiple tableViews). The second specifies the tableColumn. You can identify a column by using its identifier. You set this identifier in the InterfaceBuilder. Click on your tableview until a column is selected. Then set in the sidebar the Restoration ID. As last parameter you get your row. You return an object of type AnyObject. I normally return a string, I don't know whether NSNumber is also valid. A simple example implementation is the following:

func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!{
    var result = ""
           
    var columnIdentifier = tableColumn.identifier
    if columnIdentifier == "number" {
        result = "\(row+1)"
    }
    if columnIdentifier == "name" {
        result = model.persons[row].name
    }
    if columnIdentifier == "lastName" {
        result = model.persons[row].lastName
    }
    return result
}

当你想设置值时,使用

func tableView(tableView: NSTableView!, setObjectValue object: AnyObject!, forTableColumn tableColumn: NSTableColumn!, row: Int)

对象表示您应该传输到数据模型的新值.

Object represents the new value you should transfer to your data model.

在基于视图的 tableViews 中,情况有所不同.

In view based tableViews, the things lays different.

在这里,您必须在单独的 nib 文件中设计自定义视图.然后,您在初始化程序中注册笔尖.

Here you have to design a custom view in a separate nib file. Then, you register the nib in your initializer.

let nib = NSNib(nibNamed: "Example", bundle: NSBundle.mainBundle())
        self.tableView.registerNib(nib, forIdentifier: "Example")

这允许您使用 makeViewWithIdentifier:owner: 创建视图的实例然后您可以配置您的视图(我喜欢为此将 NSView 子类化)并将其作为

This allows you to create instances of your view using makeViewWithIdentifier:owner: Then you can configure your view (I like to subclasses NSView for this) and pass it back as result of

func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView!

示例实现:

func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView!{
    let view = tableView.makeViewWithIdentifier("Example", owner: self) as MyCustomView
    view.field1.stringValue = model.persons[row].name
    view.field2.stringValue = model.persons[row].lastName
    view.field3.stringValue = "\(row+1)"
    return view
}

这篇关于将数据解析为 NSTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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