Reputation: 215
I'm currently working in a project with a NSOutlineView...
I use, of course, NSCell(s) and I need to let the ability to select text inside the cell... Or at least... prevent the selection (and highlight) of the cells...
I search all options on IB, but can't found the right one...
Is there a way, programmatically or not, to prevent selection/highlighting of cell, nor let user select cell content ?
Thanks =)
Upvotes: 0
Views: 613
Reputation: 22948
Let's take a quick example like the outline view below. There are 3 columns: firstName
, lastName
, and fullName
.
In this particular example, let's say we want to only allow firstName
and lastName
to be editable while fullName
(which is potentially derived from firstName
and lastName
) is not. You could set this up in Interface Builder by checking or unchecking the editable checkbox for the table column. To do that, click 3 times on one of the table columns (not the header, but inside the outline view); this first selects the NSScrollView
, then the NSOutlineView
, then an NSTableColumn
:
You'd set the attributes like the following:
That provides a start by setting a default editable value for the entire column. If you need more control over whether a particular row's item value should be editable or not, you can use the outlineView:shouldEditTableColumn:item:
delegate method:
#pragma mark -
#pragma mark <NSOutlineViewDelegate>
- (BOOL)outlineView:(NSOutlineView *)anOutlineView
shouldEditTableColumn:(NSTableColumn *)tableColumn
item:(id)item {
if ([[tableColumn identifier] isEqualToString:@"firstName"] ||
[[tableColumn identifier] isEqualToString:@"lastName"]) {
return YES;
} else if ([[tableColumn identifier] isEqualToString:@"fullName"]) {
return NO;
}
return YES;
}
If you want to control whether a particular row in the outline view is selectable (for example, you could prevent selection of a group item), you can use outlineView:shouldSelectItem:
.
- (BOOL)outlineView:(NSOutlineView *)anOutlineView shouldSelectItem:(id)item {
// if self knows whether it should be selected
// call its fictional isItemSelectable:method:
if ([self isItemSelectable:item]) {
return YES;
}
/* if the item itself knows know whether it should be selectable
call the item's fictional isSelectable method. Here we
are assuming that all items are of a fictional
MDModelItem class and we cast `item` to (MDModelItem *)
to prevent compiler warning */
if ([(MDModelItem *)item isSelectable]) {
return YES;
}
return NO;
}
Upvotes: 0
Reputation: 59287
That's not much NSCell related, maybe you're looking to implementing outlineView:shouldSelectItem:
in your delegate.
On the NSCell, setEnabled:NO
, may help too. From the documentation:
setEnabled:(BOOL)flag
The text of disabled cells is changed to gray. If a cell is disabled, it cannot be highlighted, does not support mouse tracking (and thus cannot participate in target/action functionality), and cannot be edited. However, you can still alter many attributes of a disabled cell programmatically. (The setState: method, for instance, still works.)
Upvotes: 2
Reputation: 4989
Try setting:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
You could also try overriding highlightSelectionInClipRect:, but I'm not totally sure this will work.
Upvotes: 0