Reputation: 1428
In the past, with a cell-based NSTableView
, you could modify attributes of the cell in the tableView:willDisplayCell:forTableColumn:row
method (for example, setting a custom NSAttributedString
for one of the cells to display). Is there an equivalent method for view-based table view's?
Implementing tableView:willDisplayCell:forTableColumn:row
doesn't get called in a view-based table view, so I'm not sure what to do.
I want to set a NSAttributedString
using the setAttributedStringValue
method of the default NSTextField
instance that is included in a NSTableCellView
instance created from within Xcode.
My efforts so far always get undone by the table view itself. Any ideas?
Upvotes: 14
Views: 9439
Reputation: 117
In case anyone else is having a hard time with this, I think I got it working. Oddly cellcortex's answer works using setTextColor
, but any changes made using setAttributedStringValue
on the built-in textField are still overwritten. If you subclass NSTableCellView
and make custom outlet to the NSTextField
, you can modify the attributed string value though this outlet and not have your changes overwritten.
Here's how I did it with a view-based NSOutlineView
: The outline view has a column with the identifier "Detail". This column has a subclassed NSTableCellView
(DBTableCellView
) with the identifier "DetailCellView". This DBTableCellView
has an NSTextField
inside of it. DBTableCellView
has an outlet to the NSTextField
it encompasses called "customTextField".
The outline view delegate has this method:
- (NSView*)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item{
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"Detail"]) {
DBTableCellView * cellView = [outlineView makeViewWithIdentifier:@"DetailCellView" owner:self];
NSTextField * textField = cellView.customTextField;
NSString * originalString = [textField stringValue];
if (originalString.length > 0){
[textField setAllowsEditingTextAttributes: YES];
[textField setSelectable: YES];
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:originalString];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor blueColor], NSForegroundColorAttributeName,
[NSNumber numberWithInt:NSSingleUnderlineStyle],NSUnderlineStyleAttributeName, nil];
NSRange range = NSMakeRange(0, [attrString length]);
[attrString addAttributes:attributes range:range];
[textField setAttributedStringValue: attrString];
}
return cellView;
} else { //specify other column identifiers here later
return NULL;
}
}
Upvotes: 0
Reputation: 3176
I used this to change the color of the text in viewForTableColumn
. I am reading the tableCellView from Interface builder.
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSTableCellView *result = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
result.textField.textColor = [NSColor redColor];
return result;
}
Since the textcolor actually changes for me, I believe you should be able to set other attributes there as well.
Upvotes: 9
Reputation: 28806
Apparently, NSTableViewDelegate has a few new methods:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
I assume that for a view-based NSTableView
, both of these will be called. I guess you need the first one of those.
Upvotes: 3
Reputation:
Check the NSTableViewDelegate Protocol Reference, you will the method you need there :
- (void)tableView:(NSTableView *)aTableView
willDisplayCell:(id)aCell
forTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex;
Upvotes: -8