NSTableView single click action - macos

How to capture the event of single-clicking on a row of an NSTableView and trigger an action? (Idealy, it should trigger the action for left clicks only, not right clicks, but this is not required.)
Implementing -tableViewSelectionDidChange of the table view's delegate is close to what I want. However, if a row is currently selected, clicking on that row again doesn't call -tableViewSelectionDidChange since the selection didn't change.

Connect the selector action event on the table view (better from interface designed), then
- (IBAction)onAction:(id)sender {
NSTableView* tableView = (NSTableView*)sender;
// use tableView.selectedColumn/tableView.selectedRow to get the selection
}
You can also connect it from code if you want fro viewDidLoad:
[self.tableView setAction:#selector(onAction:)];

Related

NSTableview doubleAction but call action?

I'm writing an application for Mac. In NSTableview I set both handler for action and doubleAction.
self.tableView.doubleAction = #selector(tableViewDoubleAction)
self.tableView.action = #selector(tableViewSingleAction)
The problem is every double click the row in table view, it call both 'tableViewDoubleAction' and 'tableViewSingleAction'.
How can I separate it?
Thanks,

How do you put and control a checkbox in an NSTableView?

I am looking for a way of programmatically putting a checkbox in the first column of an NSTableView. I want to do it in Swift. I need to be able to read the values of all the buttons and set the button when someone clicks the row.
Is there an easy way to do this? I can't find it...
You need the table view to be view-based, not cell-based. The easiest way IMHO is to put the checkbox into the first column of your table view with the interface builder. It should be a sibling of your text field. I assume you want the first column to just contain a checkbox, and nothing else. In this case you should hide the textfield, so that it does not interfere with the checkbox (click the "hidden" checkbox in interface builder for the textfield).
Option 1: create a subclass of NSTableCellView, name it MyTableCellViewWithCheckbox, and give it a #property (nonatomic,strong) NSButton *checkbox; Change the custom class field from NSTableCellView to MyTableCellViewWithCheckbox for the first column and connect the outlet.
Option 2: (I haven't tested it yet, but I think it should work.) Set the tag of the checkbox to 13. To retrieve the checkbox later from the table view cell, you can use the viewWithTag method.
Next, you need to attach an action to your checkbox, so you should create an IBAction onto your view controller (not onto MyTableCellViewWithCheckbox.) In the action-method you can use the tableviews rowForView method to get the row index of the checkbox the user just tapped. The new state can be received directly from the sender of the action (it is an NSButton).
Also, don't forget that awakeFromNib may be called multiple times in your view controller if you have a table view in it - once for each table view cell you instantiate (this took me some time to figure out.)
I was able to do this by using a Cell-based NSTableView. Make the TableView look like this:
Set the tableColumn to look like this:
Set the buttonView to look like this:
Set the checkbox by having func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? return 1 for a check and 0 for a non-check.

NSTableView - Selecting index by typing

BACKGROUND:
I have a tableview with a list of names. When you click on a name in the list, it displays additional detail information in another section of the window. Everything is connected and is working correctly.
However...
I would like to use Type Select with this table and have run into the following snag:
When I start typing a name (while the table is selected) it correctly highlights the appropriate name in the table BUT the detailed information to the right of the table does not change.
I know the reason is the code for changing the detail information is in an IBAction method which is only called when you click to select a name in the list, and uses the [sender clickedRow] call to get the index of the selected name.
I also suspect that I need to use the [tableView selectedRow] (since it is being selected, but you are not clicking on it) but I am not quite certain where or how to perform this check.
I'm also thinking that since "type select" isn't sending an action message, I won't be able to use [sender selectedRow] but rather will use [tableView selectedRow]...
QUESTION:
How can I tell when the selected row in a tableview has changed via type select?
Thanks!
Implement the tableViewSelectionDidChange: delegate method in your controller, and ensure that your controller is the NSTableView's delegate.
You could update your view with something like:
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
[relatedView updateInformationFromRow:[[aNotification object] selectedRow]];
}
From the documentation:
tableViewSelectionDidChange: Informs the delegate that the table
view’s selection has changed.
Within that, you would update your related view. You could use the aNotification's object, which will be a NSTableViewSelectionDidChangeNotification, again from the documentation, is:
Posted after an NSTableView object's selection changes. The
notification object is the table view whose selection changed. This
notification does not contain a userInfo dictionary.

Adding a delete button to a CPTableView column in Cappuccino

This seems like it would be an easy thing to do but I am having a lot of trouble getting a button to respond to events while in a CPTableView. Here is the initialization code:
//deleteColumn is hooked up to CIB table column.
[deleteColumn setEditable:YES];
[deleteColumn setWidth:24];
var deleteButton = [[CPButton alloc] initWithFrame:CGRectMakeZero()];
[deleteButton setTarget:self];
[deleteButton setAction:#selector(deleteClicked:)];
[deleteColumn setDataView:deleteButton];
I then have this selector code in the same view controller:
- (void)deleteClicked:(id)sender
{
console.log(sender);
}
It seems the table view is squashing any mouse clicks inside it because I don't get the console log when I click the button.
Is there an easy way to do this? All I want is a button that deletes corresponding row in the table.
The CPTableView takes over the action of the button for its own purposes. Try listening for the regular edit delegate message CPTableViewDataSource:tableView:setObjectValue:forTableColumn:row: in your table delegate.

How to add an item to an array by clicking a plus navigationBar button

I have an application with a view that have a tableView which is populated with strings from an array, and also have a navBar with a plusButton. What I want is that when I press the plusButton I want to be able to add items to that array.
Add action method (IBAction) to that UIButton that is implemented in table view control class.
Implement in that method adding new object to your array
Reload table view (or call appropriate methods to add row one by one) after modifying array

Resources