View-based NSOutlineView header cell font issues

自作多情 提交于 2019-12-04 12:36:47

问题


I'm currently trying to use a new view-based NSOutlineView in my Cocoa app. As I'm not using bindings, so I implemented all required delegate and datasource methods in my controller.

In interface builder I've added a NSOutlineView with a highlighting set to SourceList and Content Mode set to View Based. Thus, there were two default table cell views provided (one Header cell with HeaderCell set as identifier and one data cell with DataCell set as identifier)

This is what it looks like in interface builder, header cell views correctly show a grey-blue textField while data cell views have a image view and a textField with correct color and font settings

To provide the views, I use the following code, to return a DataCell-view or a HeaderCell-view and set the textField of the cell accordingly, based on the corresponding identifier set in interface builder.

- (NSView *)outlineView:(NSOutlineView *)outlineView 
     viewForTableColumn:(NSTableColumn *)tableColumn 
                   item:(id)item {


    NSTableCellView *result = nil;

    if ([item isKindOfClass:[NSMutableDictionary class]]) {
        result = [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];

        id parentObject = [outlineView parentForItem:item] ? [outlineView parentForItem:item] : groupedRoster;
        [[result textField] setStringValue:[[parentObject allKeys] objectAtIndex:0]];


    } else {
        result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];

        [item nickname] ? [[result textField] setStringValue:[item nickname]] : [[result textField] setStringValue:[[item jid] bare]];
    }
    return result;
}

Running everything it looks like the following.

Could anybody provide me with hints, to why the header cell is neither bold, nor correctly colored, when selected?


回答1:


You need to implement the -outlineView:isGroupItem: delegate method and return YES for your header rows. That will standardize the font and replace the disclosure triangle on the left with a Show/Hide button on the right. You will still need to manually uppercase your string to get the full effect.

I'm not sure if the group row delegate method above makes the selection style look okay or not. However, you normally don't want the header rows to be selectable at all in source lists, which you by returning NO for header items from the -outlineView:shouldSelectItem: delegate method.




回答2:


I have created a little sample project which includes a source list and also uses the -outlineView:isGroupItem: method as @boaz-stuller has suggested.

  • Display a list of items
  • Edit the items in a master-detail fashion
  • Remove and add items
  • Usage of bindings

Check out besi/mac-quickies on github. Most of the stuff is either done in IB or can be found in the AppDelegate



来源:https://stackoverflow.com/questions/7097142/view-based-nsoutlineview-header-cell-font-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!