tableview mousedown

I have subclassed some cells of a tableview in order to display an icon corresponding to a PDF file on my computer

if I click on a row of my table, then this row is selected, this is correct

but I also want to click on the subclassed cell, in order to open the PDF file in acrobat or other readers

I know how to open the file, but I don't know how to receive the mouseDown event in the subclassed cell


Thank's for help

In the function didSelectRow, you can test for the cell.


if it is the subclass type, then open the pdf.

Otherwise, do as you need for the parent class.

it would be almost good, but I forgot to tell my App is for macOSX, so the table is a NSTableView and I don't see any didSelectRow function


Even if the function exists somewhere, it would not be such a good solution, because the click of the column can be only the first time the row will be selected. After, the corresponding row would be selected and the function will not be called anymore

For OSX, the delegate func is

func tableViewSelectionDidChange(_ notification: Notification) { }


It is true that the func is not called if the same row is clicked.

So, if you don't mind not seeing the selected cell highlited, you can call tableView.deselectRow(row) at the end of selecteionDidChange

You can now click again the same cell and get notified.

A single click on a row to perform an action is a kind of lousy UI. It has the opposite problem from the one you raised: there would then be no way to just select the row. (You may have no use for row selection right now, but blocking its possible future use doesn't seem wise.) In effect, you're changing the row into one big button, without making it look like a button.


A better solution is to put an actual button in the row, and have the "open PDF" action require a click on the button.


Alternatively, you can provide the "open PDF" action in response to double-clicking the row. Note that the table view has a double-click action that you can set in IB or programmatically. In that case, the action method can check the table view's "clickedRow" property to find out which row was double-clicked.


(The table view also has a normal click action, like all controls do. If you really, really want to go that way, you can set that to an action method that checks "clickedRow" to decide whether to open the PDF. Or, since you've subclassed the cell view, you can do whatever you want in the cell to handle mouse events, so that's another avenue. But I'd strongly recommend not doing anything weird, but using a conventional, boring, time-tested UI choice.)

Effectively, I need to click on the row to have selection on it and doubleclick for an action also

AND I also wants that if just the cell is clicked, the PDF will be open (nevertheless the row is selected or not)


I understand what you proposed (to put a button instead of a text in tje cell), but I have to admit I'm just a beginner in swift and mac OSX programming, and I don't know how to transform this cell in a special cell containing button.


I suppose I have to make the operation in tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

}


bu I don't know how to construct this special NSview

You said you already subclassed some table cells (subclassed NSTableCellView, I assume). I assume you also put prototypes for these subclassed cells in the table view in IB, and gave them a unique identifier. Is that correct?


For those subclass prototypes, you can simply drag a button from the object library into the prototype view. Place it where it looks right, then add auto-layout constraints to keep it in the right place, relative to the other things in the cell (icon/text/whatever). Then select the button, and connect its action to the view controller where the action method is implemented, or to First Responder (which means the target will be found at run time by traversing the responder chain). In the action method, you would check the clickedRow to find out which row's PDF to open.


That's a quick description. If it's unclear or I've missed anything, I'd be happy to clarify.

It took me some time to understand, but that's it

I have put a button in my cell and follow the mouseDown event for this button

If I click, I open the goof PDF file.

Thank's a lot

tableview mousedown
 
 
Q