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

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.

Related

NSTableView single click action

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 I set a property when created with bindings?

I'm trying to build a To-do list application. I have 2 tablesviews and one textfield. In the first tableview are the different projects, and when you click on one of them the associated todos appear in the second tableview. It's a pretty basic Master-detail I guess.
I set it all up with bindings.
Right now the way you add a task, is you click on an add button and it adds a row with a placeholder text that's editable.
But what I want, is the user to enter the task in the textfield, press add, and then it adds the todo with the name already set.
So basically I have TodoItem Class with a name property, and my question would be, how do I get the content of the nstextfield and assign it to the name property ?
I tried creating an outlet from the Todoitem class to the textfield, but xcode won't let me connect it....
Tell me if you need to see any code, but since I used bindings, there's almost nothing to show. Thanks!
… how do I get the content of the nstextfield and assign it to the name property ?
Translate that directly into Objective-C:
NSString *contentOfTheNSTextField = [myTextField stringValue];
myNewTask.name = contentOfTheNSTextField;
You'd do that in the action method that you've set both the button and the field to call.
I tried creating an outlet from the Todoitem class to the textfield, but xcode won't let me connect it....
To do this, the Todoitem would need to reside in the nib.
But, even if you could do that, why should the model object know about the text field? Carrying values between model and view is a controller's job.

How to identify UITableView rows in Storyboard?

I used to be able to use UITableViewCells, edit those as I wanted, link them to the .h file, then all I would need to do is call them in code. But now, how would I declare a cell built in the table in storyboard that needs to be called in code?
Select the cell you are interested in and display the Attributes inspector. There is a field named Identifier. Put a unique string like "CellSpecial" in this field.
Then in cellForRowAtIndexPath: load the cell like this:
UITableViewCell *pCell = [tableView dequeueReusableCellWithIdenitifier: #"CellSpecial"];
And if you are creating these cells dynamically (ie, the cell order of the executing application is different than that of the storyboard), then you must go to the TableView in Storyboard and set the Content attribute to Dynamic Prototypes (not Static Content).

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.

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