NSTableView with a custom view?

365 views
Skip to first unread message

James Waldrop

unread,
Oct 25, 2014, 10:18:25 PM10/25/14
to swift-l...@googlegroups.com
The responses to my last question were so great (thanks folks!) that I'm back for a little more. I've been digging around on this for 24 hours now and I've learned a lot, but haven't managed to get to where I'd like to be.

Context: MacOS 10.10 Swift project made with XCode 6.1, with the initial controller of my storyboard having a single-column NSTableView occupying the whole thing.

I have a custom view controller in my storyboard, not hooked up to anything but identified as "MyCustomViewController". For prototyping purposes I have a single NSTextField that says "Got Here!" inside the view that's inside the view controller. I'm trying to get this to be rendered as the row in my table view. In the code below, if 'simple' is true, I get an NSTextField rendered that says "Got Here!", with viewForTableColum and objectForTableColumn called. If 'simple' is false, I get a blank table view and only viewForTableColumn is called. I am successfully loading the custom view controller, though. I figure I'm missing something simple, but a day's worth of looking at other people's examples hasn't helped so far. :/  Worth noting that I also experimented with creating a separate nib with just a view in it, no view controller, but that ended up being a deep rat hole that I couldn't escape (I couldn't get it initialized properly).


TIA for any help you can provide!


class ViewController: NSViewControllerNSTableViewDataSourceNSTableViewDelegate {

    let simple = true

    

    @IBOutlet var tableView: NSTableView!

    

    override func viewDidLoad() {

        super.viewDidLoad()


        tableView.setDataSource(self)

        tableView.setDelegate(self)

    }

    

    func numberOfRowsInTableView(tview: NSTableView) -> Int {

        return 1

    }


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

        println("tableview objectValueForTableColumn called")

        return "Got Here!"

    }


    func tableView(tview: NSTableView, viewForTableColumn col: NSTableColumn?, row: Int) -> NSView? {

        var result: NSView?

        

        println("tableview viewForTableColumn called")

        if (simple) {

            result = NSTextField()

        }

        else {

            if let customVC = storyboard?.instantiateControllerWithIdentifier("MyCustomViewController"asNSViewController {

                result = customVC.view

            }

            else {

                println("no storyboard")

            }

        }

        return result

    }   

}

James Waldrop

unread,
Oct 27, 2014, 2:01:10 AM10/27/14
to swift-l...@googlegroups.com
So, I finally figured out how to just make the table cell view have the stuff I want in it. Now I'm trying to work out how to populate the values for those views, since outlets obviously won't do the trick. I suspect I'm going to end up using bindings for this, but am still diving in on that.

For future generations trying to retrace my (bad) path -- I finally found enlightenment watching the WDC 2011 video where view-based table views were introduced. This video is very well done. Here's a link to all the presentations, and the presentation I'm specifically getting so much insight from is entitled "View Based NSTableView Basic to Advanced":

Reply all
Reply to author
Forward
0 new messages