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,
Related
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.
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:)];
Can anyone offer assistance with adding a delete button to a tableview in xcode? I can create the button but cannot get it to delete anything in the table. I'm using the default code that comes with the Master Detail Application but have changed the content of the table using an NSObject.
This should be the default behaviour of the edit button.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
This will allow you to click and delete any item in the list after it is pressed.
Unless I am misunderstanding how you are using the table item list.
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.
I've developed an app for Mac OS X Lion using its new view-based NSTableView, but as I want to port the whole app to Snow Leopard I'm trying to figure out the best way to emulate such a tableview. So far I've created a NSCollectionView and everything is fine, except for the fact that I can't get the index of the view from which a button click event is triggered.
In Lion I have the following function:
- (IBAction)buttonClick:(id)sender
so I can get the index of the view inside the tableview using a method (I can't remember its name) like
- (NSInteger)rowForView:(NSView *)aView
with aView being the sender's superview, but I couldn't find something similar for the collection view ... The only "useful" method seems to be
- (NSCollectionViewItem *)itemAtIndex:(NSUInteger)index
(or something like this), but this can't help me as it returns a NSCollectionViewItem and I can't even access it knowing only the corresponding view!
Within buttonClick, try this code:
id collectionViewItem = [sender superview];
NSInteger index = [[collectionView subviews] indexOfObject:collectionViewItem];
return index;
Hope this helps :)
Geesh! Both of those approaches have issues. I can see how the first on may work, but note that the "collectionViewItem" is actually the view, NOT the collectionViewItem, which is a view controller.
The second way will not work, unless you subclass the button and put in a back link to the collectionViewItem. Otherwise, your view does not know what collectionViewItem controls it. You should use a selector binding to the collectionViewItem's representedObject instead, to get the action to the correct object in your array.
How about something like:
id obj = [collectonViewItem representedObject];
NSInteger index = [[collectionView contents] indexOfObject:obj];
As I suggested here: How to handle a button click from NSCollectionView
I would do it like this (because the button you want to press should be coupled with the corresponding model, therefore the represented object):
Add a method to the model of your collectionViewItem (e.g. buttonClicked)
Bind the Button Target to Collection View Item
While binding set model key path to: representedObject
While binding set selectorname to: methodname you chose earlier (e.g. buttonClicked)
Add protocol to your model, if you must tell delegate or establish observer-pattern
use NSArrayController for binding to NSCollectionView,
use collectonViewItem.representedObject to get a Custom Model defined by yourself.
save and get index in your custom model.
That's works for me.