NSOutlineView: remove disclosure triangle and indent

断了今生、忘了曾经 提交于 2019-11-28 15:50:31

问题


I'm using NSOutlineView for a project, and can't seem to figure out two things:

  • How to remove the disclosure triangle for tree nodes. Apps like iTunes seem to be able to do this:

Is there some sort of NSOutlineView Delegate method that is used for this? Or does it require a subclass?

  • How to disable indenting for items. I've tried using setIndentationPerLevel: and setting it to 0, as well as changing the column indent to 0 in Interface Builder, but it does not seem to have any effect.

回答1:


You've run into the right person here. I've had to grapple with this just a week ago.

Removing the disclosure triangle: implement the frameOfOutlineCellAtRow: method in your NSOutlineView subclass and return NSZeroRect (only if you want to hide that particular row's triangle, of course.)

- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row {
    return NSZeroRect;
}

Disable indenting: the outline view's standard layout reserves space at the far left to draw the triangles in, in case the item is expandable. But you can override that for individual items by specifying a different drawing frame. You also do that in your subclass, by responding to this message:

- (NSRect)frameOfCellAtColumn:(NSInteger)column row:(NSInteger)row {
    NSRect superFrame = [super frameOfCellAtColumn:column row:row];


    if ((column == 0) /* && isGroupRow */) {
        return NSMakeRect(0, superFrame.origin.y, [self bounds].size.width, superFrame.size.height);
    }
    return superFrame;
}



回答2:


For future reference, the cleanest and simplest way to hide the disclosure triangle in expandable NSOutlineView items is by implementing the outlineView:shouldShowOutlineCellForItem: method of the NSOutlineViewDelegate protocol in your delegate:

-(BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item
{
    // replace this with your logic to determine whether the
    // disclosure triangle should be hidden for a particular item
    return [item hidesDisclosureTriangle];
}



回答3:


I had to combine the two approaches above because outlineView:shouldShowOutlineCellForItem: alone does not remove the space reserved for the disclosure triangles (it does remove the triangles themselves).

Delegate:

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item {
    return NO;
}

Subclass of NSOutlineView:

@implementation ExpandedOutlineView

#define kOutlineCellWidth 11
#define kOutlineMinLeftMargin 6

- (NSRect)frameOfCellAtColumn:(NSInteger)column row:(NSInteger)row {
    NSRect superFrame = [super frameOfCellAtColumn:column row:row];
    if (column == 0) {
        // expand by kOutlineCellWidth to the left to cancel the indent
        CGFloat adjustment = kOutlineCellWidth;

        // ...but be extra defensive because we have no clue what is going on here
        if (superFrame.origin.x - adjustment < kOutlineMinLeftMargin) {
            NSLog(@"%@ adjustment amount is incorrect: adjustment = %f, superFrame = %@, kOutlineMinLeftMargin = %f", NSStringFromClass([self class]), (float)adjustment, NSStringFromRect(superFrame), (float)kOutlineMinLeftMargin);
            adjustment = MAX(0, superFrame.origin.x - kOutlineMinLeftMargin);
        }

        return NSMakeRect(superFrame.origin.x - adjustment, superFrame.origin.y, superFrame.size.width + adjustment, superFrame.size.height);
    }
    return superFrame;
}

@end

Result:




回答4:


Use PXSourceList. It's the style you're looking for with a very nice api.




回答5:


The correct way to specify the intention per level is by overwriting indentationPerLevel of NSOutlineView.

class NoIndentOutlineView: NSOutlineView {
    override var indentationPerLevel: CGFloat {
        get {
            return 0;
        }
        set {
            // Do nothing
        }
    }
}

This will make sure children have the same indention as the parents.




回答6:


This is in regards to hiding the disclosure button. If you're looking to globally hide it on expandable items, one can do this:

extension NSOutlineView {
    override open func makeView(withIdentifier identifier: NSUserInterfaceItemIdentifier, owner: Any?) -> NSView? {
        if identifier == NSOutlineView.disclosureButtonIdentifier {
            return nil
        } else {
            return super.makeView(withIdentifier: identifier, owner: owner)
        }
    }
}

Else you can subclass NSOutlineView and override the makeView function.

I know the q is for obj-c. I just had to work through this and my project is swift.




回答7:


I tried this in Swift. It just works. I don't know whether it is a proper way. There should be equivalent in Obj-C:

extension NSTableRowView {
    override open func layout() {
        super.layout()
        if let cell = subviews.first as? NSTableCellView, cell.objectValue is <YourHeaderCellClass> {
            subviews.first?.setFrameOrigin(NSZeroPoint)
        }
    }
}

Be sure you return YourHeaderCellClass item in datasource method.

func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any


来源:https://stackoverflow.com/questions/4251790/nsoutlineview-remove-disclosure-triangle-and-indent

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