Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Data into NSTableView

Hello fellow programmers,

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.

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.

Thank you.

like image 623
Paulo Avatar asked Aug 01 '14 21:08

Paulo


1 Answers

Yes, tableviews with Cocoa are... complicated.

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.

Cell based TableVies

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

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

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
}

When you want to set values, use

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

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

View based TableViews

In view based tableViews, the things lays different.

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")

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!

Example implementation:

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
}
like image 195
ShadowLightz Avatar answered Sep 28 '22 01:09

ShadowLightz