James Waldrop
James Waldrop

Reputation: 545

Why is my NSTableView not showing the data I give it?

The setup:

  1. a Cocoa-based MacOS application created with XCode 6.1
  2. using Storyboards with
  3. a ViewController that has an NSTableView as its sole view
  4. all done with Swift as the language

My ViewController code is this:

import Cocoa

class ViewController: NSViewController, NSTableViewDataSource {
    @IBOutlet var tableView: NSTableView!

    let data = [
        ["FirstName": "Debasis", "LastName": "Das"],
        ["FirstName": "Nishant", "LastName": "Singh"],
        ["FirstName": "John", "LastName": "Doe"],
        ["FirstName": "Jane", "LastName": "Doe"],
        ["FirstName": "Mary", "LastName": "Jane"]
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.setDataSource(self)

    }

    override var representedObject: AnyObject? {
        didSet {
        }
    }

    func numberOfRowsInTableView(tview: NSTableView) -> Int {
        return data.count
    }

    func tableView(tview: NSTableView, objectValueForTableColumn col: NSTableColumn?, row: Int) -> AnyObject? {
        return "row \(row) col \(col?.identifier)"
    }
}

There is a connection from the tableView outlet to the NSTableView in the storyboard.

When this app is run, you get a a table view with 5 rows as you'd expect given the data, but the contents of each cell is "Table View Cell". I've tried just returning "1", the number 1, various other objects, but I can't get it to display what I return from the tableView method. I'm assuming that the AnyObject returned needs to be something specific (as in there's an instanceof check in the caller), but all the sample code I've found returns strings and things work, so either things have changed or my code is wrong in some way I haven't been able to figure out.

Upvotes: 0

Views: 2158

Answers (2)

James Waldrop
James Waldrop

Reputation: 545

I found the answer here, after searching the Apple Dev Forums:

NSOutlineView - object value is being returned, but only the placeholder title "Table View Cell" is displayed

Specifically the bindings approach works easily.

Upvotes: 1

Deekor
Deekor

Reputation: 9499

If you are just doing a view that contains a Table view, I recommend just using a UITableViewController instead. Create one from the template, should be much easier for you to get this up and running.

Upvotes: 0

Related Questions