0

I really apologize in advance for this question, but I am not fully understanding how tables work in an OSX app. What I'd like the user to be able to do is to select multiple rows in a single column table, and I'd like to know which rows were selected. The app populates the table, and the user cannot change it. I've been searching the web, SO, and I've cobbled together the following code, which is the controller for a single column table. I created a Table View in IB and wired its data source and delegate to File's Owner in IB. I now have a table with the array that I defined and can highlight the rows:

enter image description here

The controller code is as follows:

import Cocoa

class MainWindowController: NSWindowController {

    let mylist = [ "worm", "pickle", "ladybug" ]

    override var windowNibName: String {
        return "MainWindowController"
    }

    override func windowDidLoad() {
        super.windowDidLoad()
    }
}

// NSTableViewDataSource
extension MainWindowController: NSTableViewDataSource {
    func numberOfRowsInTableView( aTableView: NSTableView ) -> Int {
        return mylist.count
    }

    func tableView( tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
        var cellView: NSTableCellView = tableView.makeViewWithIdentifier( tableColumn!.identifier, owner: self ) as! NSTableCellView
        cellView.textField!.stringValue = mylist[ row ]
        return cellView
    }
}

// NSTableViewDelegate
extension MainWindowController: NSTableViewDelegate {
}

What I don't understand is how to allow the user to select multiple rows and how to access which rows are selected. I have a feeling that I'm missing something simple, but I am just stumped. Any help would be very much appreciated.

Dribbler
  • 4,343
  • 10
  • 33
  • 53
  • 1
    You allow for multiple selection in Interface Builder, then you do something like that: http://stackoverflow.com/questions/29198210/nstableview-selected-row-swift/29200067#29200067 – Eric Aya Jan 24 '16 at 18:10
  • @EricD -- it is amazing how many search terms I entered and didn't come across that! Many thanks... bringing the code in to test now... Do you know where multiple selection lies? I've combed IB a bunch of times and haven't found it yet... – Dribbler Jan 24 '16 at 18:14
  • 1
    You're welcome. :) Allowing multiple selection is just clicking a checkbox in the Attributes Inspector (panels on the right in Xcode, fourth one) when your table is selected in IB. – Eric Aya Jan 24 '16 at 18:16
  • 1
    I'm now closing this question as a duplicate of the one I answered and linked in my comment, if you're ok with that. – Eric Aya Jan 24 '16 at 18:21
  • @EricD, totally OK with that. You're a lifesaver :) – Dribbler Jan 24 '16 at 18:22

0 Answers0