LG Coetzee
LG Coetzee

Reputation: 13

Swift Core Data displayed in OS X Tableview

I am new to xCode and Swift. I used to develop software using Visual Studio - Visual Basic.

I am browsing through tutorials and tutorials and tutorials but nowhere can I find an example of how to display a Core Data Entity created in Swift in a NStableview MAC OS X Application. All the tutorials are for iOS application.

I have created the savings methods but all I need to do is to display my saved records in a tableview. HOW CAN I DO THAT? Are there anybody out there that can give me advice on this.

Upvotes: 1

Views: 3525

Answers (1)

Dejan Skledar
Dejan Skledar

Reputation: 11435

Preparation

First of all, your ViewController must inherit from NSTableViewDelegate and NSTableViewDataSource:

class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource 

Then, like in iOS you must implement the delegate methods for the NSTableView.

Make an IBOutlet, something like:

@IBOutlet weak var tableView: NSTableView!

Delegate and DataSource

Then in the viewDidLoad(), set the delegate and the dataSource to self:

tableView.setDelegate(self)
tableView.setDataSource(self)

And implement the required methods:

For the number of rows:

func numberOfRowsInTableView(tableView: NSTableView) -> Int {
    return yourData.count //your data ist the array of data for each row
}

For the row data:

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

    if tableColumn!.title == "firstColumnTitle" //if you have more columns
    {
        return yourData[row].first
    }
    else //second column
    {
        return return yourData[row].second
    }

}

For selection: (optional)

func tableViewSelectionDidChange(notification: NSNotification) {
    var row = tableView.selectedRow
    // a row was selected, to something with that information!
}

If you have any questions, don't hesitate!

Upvotes: 4

Related Questions