Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have asked this question once before, but I'm just not very satisfied with the solution.

Automatically adjust size of NSTableView

I want to display a NSTableView in a NSPopover, or in a NSWindow.
Now, the window's size should adjust with the table view.

Just like Xcode does it:


enter image description here enter image description here


This is fairly simple with Auto Layout, you can just pin the inner view to the super view.

My problem is, that I can't figure out the optimal height of the table view. The following code enumerates all available rows, but it doesn't return the correct value, because the table view has other elements like separators, and the table head.

- (CGFloat)heightOfAllRows:(NSTableView *)tableView {
    CGFloat __block height;
    [tableView enumerateAvailableRowViewsUsingBlock:^(NSTableRowView *rowView, NSInteger row) {
        // tried it with this one
        height += rowView.frame.size.height;

        // and this one
        // height += [self tableView:nil heightOfRow:row];
    }];

    return height;
}

1. Question

How can I fix this? How can I correctly calculate the required height of the table view.

2. Question

Where should I run this code?
I don't want to implement this in a controller, because it's definitely something that the table view should handle itself.
And I didn't even find any helpful delegate methods.

So I figured best would be if you could subclass NSTableView.
So my question 2, where to implement it?


Motivation

Definitely worth a bounty

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
647 views
Welcome To Ask or Share your Answers For Others

1 Answer

This answer is for Swift 4, targeting macOS 10.10 and later:

1. Answer

You can use the table view's fittingSize to calculate the size of your popover.

tableView.needsLayout = true
tableView.layoutSubtreeIfNeeded()

let height = tableView.fittingSize.height

2. Answer

I understand your desire to move that code out of the view controller but since the table view itself knows nothing about the number of items (only through delegation) or model changes, I would put that in the view controller. Since macOS 10.10, you can use preferredContentSize on your NSViewController inside a popover to set the size.

func updatePreferredContentSize() {
    tableView.needsLayout = true
    tableView.layoutSubtreeIfNeeded()

    let height = tableView.fittingSize.height
    let width: CGFloat = 320

    preferredContentSize = CGSize(width: width, height: height)
}

In my example, I'm using a fixed width but you could also use the calculated one (haven't tested it yet).

You would want to call the update method whenever your data source changes and/or when you're about to display the popover.

I hope this solves your problem!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...