Make NSTableView NOT edittable - objective-c

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

Related

How to bring the custom view cell above table rows in NSTableView?

When I use a custom view as the cell of a view-based NSTableView, the custom view is somewhat below the table row. When I click on it, instead of affecting the elements (e.g. text field) custom view, the table row was selected (and highlighted). I have to reclick to select the text field.
- (NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSLog(#"We are creating views!");
NSTableCellView *newView;
newView = [tableView makeViewWithIdentifier:#"PostCell" owner:self];
NSTextField *newTextField = [[NSTextField alloc] init];
[newView addSubview:newTextField];
return newView;
}
When I disable the row selection according to NSTableView - Disable Row Selection, there was no selection.
- (BOOL)selectionShouldChangeInTableView:(NSTableView *)tableView {
return NO;
}
But I still cannot select directly the text field. What's worse, I cannot even select it using the mouse. Only tab on the keyboard works.
There seem to be something above it. But is it the "table column" shown in interface builder? Or something else?
How can I fix this?
Use a custom subclass of NSTableView and override -validateProposedFirstResponder:forEvent: to return YES.
See this blog entry from the Apple engineer who wrote the view-based table view code.
Make sure following code is present.
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex {
return YES;
}
You may try logging the subviews Or you can check superviews of view.
This will help to understand view hierarchy.
Also on side note if one of the view's userInteraction is disable then it's subview's won't be able to receive the events. Please verify that all the views and it's subviews userInteraction is enable.
I hope this helps.

View-based NSOutlineView and displaying Header Cells

I have been trying unsuccessfully to use so-called "Header Cells" in my view-based (which I'd like to keep that way) NSOutlineView without success.
I have tried the following things:
I have successfully set up -(BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item for the object I want to make headers.
I have thoroughly tested my delegate and data-source, so we can assume they are set up properly.
When it comes to -(NSView *)outlineView:(NSOutlineView *)ov viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item definition, I am stuck.
I had the feeling from my readings that there are things to set up in IB, but I can't figure out what. What is this #"HeaderCell" referring to?
I tried this kind of stuff which are not successful at all (displays an empty-view)
-(NSView *)outlineView:(NSOutlineView *)ov viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{ NSTableCellView *result = nil;
result = [ov makeViewWithIdentifier:#"HeaderCell" owner:self];
[[result textField]setStringValue:#"myString"];
return result;
}
What am I missing ?
Source : View-based NSOutlineView header cell font issues
The #"HeaderCell" refers to the Table Cell View identifier. In IB select the Table Cell View representing the Header Cell and on the utility sidebar select identity inspector...you should see the identifier field.
NB : From Cory's Answer
Here's a graphical explanation of how to set it up in Interface Builder :
In IB select the Table Cell View representing the Header Cell and on the utility sidebar select identity inspector...you should see the identifier field

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.

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.

NSButtonCell as Checkbox in a NSTableVIew don't get selected

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.