1

I'm writing an application with Swift that's using an NSTableView as its main interface. The application presents information from a very large array just like a spreadsheet. How can I let the user select a single cell from the NSTableView?

I've read https://stackoverflow.com/a/20807563/2771733 which states:
It sounds like you're trying to make a spreadsheet. That's not what a table view is. But there's no real solution provided other than "You're best off implementing a custom view" – which seems like a lot of work and is way beyond my current knowledge.

One idea I came across was to disable selecting at all and provide my own mouse handler but I wasn't able to switch off selecting. Even when unchecking all boxes in the Selection section in the Attribute Inspector for the Table View, a user can still select (highlight) a row. And that would interfere with my own mouse handle code.

So, is there any way to (ab)use a NSTableView as a spreadsheet? Or any other hints how I could let a user just select a single cell instead of a whole row?

Update: The answer to the possible duplicate question is just a single sentence recommending NSCollectionView for the task, but does not provide any reasons why. Especially my task to present "information from a very large array just like a spreadsheet" isn't addressed. For example, the NSCollectionViewDataSource protocol enforces you to implement collectionView:numberOfItemsInSection:. But the notion of a (inherently one-dimensional) "number of items" doesn't fit well with the idea of a spreadsheet with its two-dimensional data. The same holds true for collectionView:itemForRepresentedObjectAtIndexPath: where the index path gives you the item index within that section. Again I have to deal with one-dimensional indices for a two-dimensional source.

What I'm looking for is a native solution for rather simple grid views other GUI toolkits provide like FLTK's Fl_Table or wxWidget's wxGrid.

z80crew
  • 1,150
  • 1
  • 11
  • 20
  • Possible duplicate of [How to get index of currently selected row and column in NSTableView](https://stackoverflow.com/questions/50722758/how-to-get-index-of-currently-selected-row-and-column-in-nstableview) – Willeke Jun 09 '18 at 02:29

1 Answers1

1

Here's what I know.

To disable NSTableView row selecting, use either of this two methods in NSTableViewDelegate will work:

optional func tableView(_ tableView: NSTableView, selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet) -> IndexSet
optional func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool

And to select only a single cell, you can subclass NSTableView and override

func validateProposedFirstResponder(_ responder: NSResponder, for event: NSEvent?) -> Bool

to determine which view or control in a table need to respond to incoming events. You can read Apple's doc for more detail about this.

And after your cell is able to handle incoming events, the 'mouse handle code' in your cell you mentioned can be handled.

Tim
  • 66
  • 2