NSButtonCell as Checkbox in a NSTableVIew don't get selected - objective-c

I have a NSTableVIew for multi-selection purposes with two columns, the first one with a NSButtonCell as checkbox and the other one as a title.
The idea is to check the items to be added afterwords to an array.
The problem is that the checkboxes don't change its state when I click them. I've tried to attach an IBAction but the sender to de action is the TableView but not the checkbox
Any ideas (or link) about how to achieve this kind of functionality?

Going on the assumption you're using a NSTableViewDataSource you need to implement three methods:
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
When the checkbox is clicked the first method is called. If aTableColumn has your checkboxes you would save the new state, which is [anObject boolValue].
When the table needs to draw a row, it calls the second method. When the table column is your checkbox column, return the state that you saved in the first method.
The last method tells the table view how many rows there will be.
See the table data source documentation for more details, but I've pretty much summarized it here.

Related

NSTableView Multiple Row Types

I'm trying to create a homework planner app that has two types of TableCellViews in a View Based NSTableView. One type is a narrow bar that just has a label of what subject the below homework is for, and the other type is a row to input homework items. (I'll include a screenshot below.)
My question is: when creating new rows in a TableView, how do you specify which type of row you'd like to create? I'm assuming it has something to do with identifiers, but I can't find any information on how to use them in this way.
This is basically how it would look:
You are on the right track with the identifiers. Here's how you use them.
First setup your NSTableView with your specific row types (as you've probably already done). In the screenshot below I made one row with a title and description and another with a few buttons.
Next, you need to setup the desired identifiers. Click the first row in Interface Builder and select the Identity Inspector. Pick a unique identifier for your first row. Do the same for the other(s).
Finally, in your implementation create a new row of a specific type using the following code:
TableViewController.m
#pragma mark - NSTableViewDelegate
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
NSTableCellView *cell;
if(someCondition == YES) {
cell = [self.tableView makeViewWithIdentifier:#"ButtonRow" owner:self];
} else {
cell = [self.tableView makeViewWithIdentifier:#"TitleDescriptionRow" owner:self];
}
return cell;
}
If you're looking for a more in depth tutorial, check out Cocoa Programming L51 - View-Based NSTableView (YouTube video, not by me).

Hide button in NSTableView

I am a little stuck and can't seem to work this out from the apple docs.
I have two buttons in an NSTable column, contained within an NSTableCellView.
I am trying to, in code, hide the button depending on the values of the object in the row.
Getting the values is fine, but i can't work out how to target the specific button, i can't bind it to an outlet as it's within a table. I have tried the below code, but that just hides the entire NSTableCellView rather than the specific button, i have also tried changing the identifier to be of the button, but that seems to do the same.
if(selectedTweet.imageURL){
NSButton *imageButton = [tableView makeViewWithIdentifier:#"secondButtons" owner:self];
[imageButton setHidden:NO];
return imageButton;
} else {
NSButton *imageButton = [tableView makeViewWithIdentifier:#"secondButtons" owner:self];
[imageButton setHidden:YES];
return imageButton;
}
This is obviously much simpler than i am making it?? Help greatly appreciated.
Thanks
Gareth
If you are using CocoaBindings to populate that table, you can just bind the button's "Hidden" attribute to Table Cell View/objectValue.imageURL and use the NSIsNil value transformer. No code needed at all.
If you are using an old-fashioned data source, things become a tad more complicated. In that case you could have a imageButton property in your object, and set it in the NSTableViewDelegate's - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row method.

How can I make the Table View non-editable in Cocoa

I dragged a Table View from the library, and linked the datasource and delegate, it worked. But when the Application is running, I can double click the row in the table, and actually put some words in it. I just want it to be non-editable. What should I do?
Implement the following delegate method that will return NO
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return NO;
}
You can just uncheck the Editable button in table column attributes in xib file.

Make NSTableView NOT edittable

OK, so here's my situation :
I've got an NSTableView.
Its columns are bound to an NSArrayController.
... which in turn is bound to an NSMutableArray.
However :
When the user click in any of the cells, he's allowed to edit their content.
We DON'T want that.
How should I do it, so that no table cells are editable?
In your NSTableViewDelegate return NO for this method:
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

An NSOutlineView with an empty datasource doesn't invoke its delegate methods?

Differently from a NSTableView, the delegate methods of an empty NSOutlineView are never invoked.
For instance, NSTableView method:
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
is invoked even if there are not rows.
But, NSOutlineView method:
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
is not invoked if there are not items.
I need to write arbitrary text on the first row of my outlineView when it is empty. But I can't use the above mentioned method like I do for NSTableView.
What's the solution ?
thanks
Have you implemented outlineView:numberOfChildrenOfItem: to give a correct answer when passed nil?
From your description it seems to me that it should return 1 (your initial line) when passed nil, but I can't be sure without more code and/or information.