自动调整 NSTableView 的高度 [英] Automatically adjust height of a NSTableView

查看:27
本文介绍了自动调整 NSTableView 的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经问过这个问题,但我对解决方案不太满意.

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

自动调整NSTableView的大小

我想在 NSPopoverNSWindow 中显示 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.

就像 Xcode 一样:

Just like Xcode does it:

使用自动布局非常简单,您只需将内部视图固定到超级视图即可.

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.问题

我该如何解决这个问题?如何正确计算表格视图所需的高度.


1. Question

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

我应该在哪里运行这段代码?
我不想在控制器中实现它,因为它绝对是表格视图应该自己处理的东西.
而且我什至没有找到任何有用的委托方法.

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.

所以我想最好的办法是你可以继承 NSTableView.
那么我的问题2,在哪里实现呢?

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

绝对值得赏金

推荐答案

此答案适用于 Swift 4,针对 macOS 10.10 及更高版本:

1.回答

您可以使用表格视图的 fittingSize 来计算弹出框的大小.

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.回答

我理解您希望将该代码移出视图控制器的愿望,但由于表视图本身对项目数量(仅通过委托)或模型更改一无所知,因此我会将其放在视图控制器中.从 macOS 10.10 开始,您可以在弹出框内的 NSViewController 上使用 preferredContentSize 来设置大小.

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!

这篇关于自动调整 NSTableView 的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆