NSTableView - select row and respond to mouse events immediately [duplicate] - macos

This question already has answers here:
Respond to mouse events in text field in view-based table view
(6 answers)
Closed 9 years ago.
I have a view based NSTableView in which the cells contain a number of controls including text fields and edit fields. When a user tries to click on a control within a cell in order to, for example, start editing a text field, the click's main objective is ignored and the cell gets selected. Another click is then needed to perform the action originally intended, and even this click is subject to a delay before it's taken into account.
How can I avoid this problem and have the row selected and the mouse event forwarded to the control in one go?

I solved this issue by subclassing NSTableView:
#implementation QuickResponseTableView
- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event
{
// This allows the user to click on controls within a cell withough first having to select the cell row
return YES;
}
#end

Had the same problem. After much struggle, it magically worked when I selected None as against the default Regular (other option is Source List) for the Highlight option in IB! The accepted answer appears to be more specific but a little hacky compared to this.

Related

NSTableView textfield cell should not go into edit on click

I have two NSTableViews in my app and both are set up the same, same parameters, etc., they have several columns with NSTextField cells and the first column is editable.
The first table behaves like I want it: if the user clicks, the row is selected. If the user clicks the row a second time the textfield goes into edit mode, letting the user change the text in it.
The second table should act the same but it doesn't: if I click a row in it, most of the time it goes straight into edit mode with the textfield. Very, very rarely this does not happen.
Does anyone know what causes this? I checked all parameters (in IB) and code and they are the same on both tables. If I set 'Refuses first responder' on the textfield in the naughty table, it doesn't let me edit the textfield at all so that option doesn't help.
SOLVED IT! The reason why it works on the first table view but caused the edit issue on the second table was because the first table allows dragging while the second did not! It seems somehow this interferes with doubleAction (I even read somewhere that an editable table cannot have doubleAction but it seems to work without problems). I could fix it by implementing tableView(_:writeRowsWithIndexes:toPasteboard:) in the second tableview's view controller and simply return false (since the 2. table should not have drag'n'drop ability) ...
func tableView(aTableView:NSTableView, writeRowsWithIndexes rowIndexes:NSIndexSet, toPasteboard pboard:NSPasteboard) -> Bool
{
return false;
}

Editable NSTextField in NSTableView

I have a view based NSTableView with some labels in my customized and subclassed view. One of the label should be editable, so therefore I set this NSTextField to editable.
But now I have two problems, I can't solve:
1) If I move the mouse over the editable NSTextField, the cursor don't change to the IBeamCursor (the edit cursor).
2) I need to double click at the label, to be able to edit. I want to have a single click. I found some solutions for this problem here at stackoverflow, the best one is to override the acceptsFirstResponder to return always true, but then, clicking at the NSTextField selects the whole text instead of placing the cursor at the clicked position.
Sorry... this is a duplicate. I found this:
NSTableView - select row and respond to mouse events immediately
You have to subclass NSTableView. My swift code:
class TableViewEditing: NSTableView {
...
override func validateProposedFirstResponder(responder: NSResponder, forEvent event: NSEvent?) -> Bool {
return true
}
}
EDIT:
Just one disadvantage: Sometimes entering the edit mode, it seems that the text is just shortly selected and deselected. But you can see, that this is a cocoa problem, it's the same for example in Apple reminders app.

NSOutlineView NSTableRowView Loses Apparent Selection After Editing

I have a view-based outline view (OSX 10.7). Clicking on an item selects it, as usual. Double clicking allows editing of the textfield it contains. However, when I'm done editing the textField, the row's highlight disappears. The outlineView still thinks the row is selected, and sending that row a drawSelectionInRect message doesn't change its appearance. Telling the outlineView to again select the row also doesn't change its appearance. Only by again clicking on the row can I get the highlight to reappear. Any idea what's going on?
My fault. I'm observing changes to the managedObjectContext, and was reloading the entire tree when individual items changed: by correcting this to reload only the affected item, things work as they should.

Prevent selection change in NSOutlineView when NSActionCell clicked

I have a custom NSActionCell used to render some parts of some of the rows in my NSOutlineView. I can receive and respond to clicks on the NSActionCell, but the selection also changes when that cell is clicked. I'd like to prevent the selection from changing if one of my custom NSActionCells are clicked.
Is there an easy way to do this?
To answer my own question:
If the cell you want to be able to click (and subsequently not select a row) is in it's own column, then the following Apple example is very useful:
DragNDropOutlineView
That example relies on implementing the following NSOutlineViewDelegate method (implemented in AppController.m at line 304):
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
If you have a cell within another cell, you can still use that approach, but you'll need to do a bit more work to determine whether the mouse was clicked within your sub-cell. A good example demonstrating that logic is the following Apple example:
PhotoSearch

NSTableView handling edititng cells correctly

I have an NSTableView working correctly except when I'm editing one of the table items. If the user is still in edit mode, and it presses the Sheet OK button, the tableiew doesn't update.
How do I force the tableview to commit the changes when the user press the ok button (closesheet).
Also, how do I handle the ESC Key to cancel the editing?
Sorry if the questions looks absurd, but I've been only on developing on Mac for a month.
You should be able to call [sheet makeFirstResponder:sheet], where sheet is the sheet you are about to close. That will switch first responder status away from the text field, which will cause it to commit the in-progress edit.
I had the very same problem: How to add data from an NSTextField to a Core Data Attribute without having to press Return or Tab after editing the TextField?
There seem to be different solutions. If you use bindings to connect the NSTextField of your edit sheet to the ArrayController, you could check „continously update value“ in the TextFields value binding in Interface Builder.
Other solutions are explained very well in this blogpost at Red Sweater Blog: http://www.red-sweater.com/blog/229/stay-responsive

Resources