14
votes

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?

4
Did you set the class that has that method as delegate? - Rudy Velthuis
Yes I set the delegate correctly. That method doesn't get called when you are using a view-based table view. - willbur1984

4 Answers

9
votes

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.

3
votes

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.

0
votes

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;

  }

}
-8
votes

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;