• <tfoot id='UupL8'></tfoot>

        <bdo id='UupL8'></bdo><ul id='UupL8'></ul>

        <i id='UupL8'><tr id='UupL8'><dt id='UupL8'><q id='UupL8'><span id='UupL8'><b id='UupL8'><form id='UupL8'><ins id='UupL8'></ins><ul id='UupL8'></ul><sub id='UupL8'></sub></form><legend id='UupL8'></legend><bdo id='UupL8'><pre id='UupL8'><center id='UupL8'></center></pre></bdo></b><th id='UupL8'></th></span></q></dt></tr></i><div id='UupL8'><tfoot id='UupL8'></tfoot><dl id='UupL8'><fieldset id='UupL8'></fieldset></dl></div>
        <legend id='UupL8'><style id='UupL8'><dir id='UupL8'><q id='UupL8'></q></dir></style></legend>

      1. <small id='UupL8'></small><noframes id='UupL8'>

        自动调整 NSTableView 的高度

        时间:2023-09-12
            <tbody id='BnI0o'></tbody>

            <legend id='BnI0o'><style id='BnI0o'><dir id='BnI0o'><q id='BnI0o'></q></dir></style></legend>

            <small id='BnI0o'></small><noframes id='BnI0o'>

          1. <i id='BnI0o'><tr id='BnI0o'><dt id='BnI0o'><q id='BnI0o'><span id='BnI0o'><b id='BnI0o'><form id='BnI0o'><ins id='BnI0o'></ins><ul id='BnI0o'></ul><sub id='BnI0o'></sub></form><legend id='BnI0o'></legend><bdo id='BnI0o'><pre id='BnI0o'><center id='BnI0o'></center></pre></bdo></b><th id='BnI0o'></th></span></q></dt></tr></i><div id='BnI0o'><tfoot id='BnI0o'></tfoot><dl id='BnI0o'><fieldset id='BnI0o'></fieldset></dl></div>

                <bdo id='BnI0o'></bdo><ul id='BnI0o'></ul>
                <tfoot id='BnI0o'></tfoot>
                • 本文介绍了自动调整 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 的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:自动布局约束:如何使视图在调整大小时保持其宽度/高度比? 下一篇:方向更改后 UITableView 布局中的错误

                  相关文章

                • <tfoot id='phdUC'></tfoot>
                    <bdo id='phdUC'></bdo><ul id='phdUC'></ul>

                  1. <legend id='phdUC'><style id='phdUC'><dir id='phdUC'><q id='phdUC'></q></dir></style></legend>
                    <i id='phdUC'><tr id='phdUC'><dt id='phdUC'><q id='phdUC'><span id='phdUC'><b id='phdUC'><form id='phdUC'><ins id='phdUC'></ins><ul id='phdUC'></ul><sub id='phdUC'></sub></form><legend id='phdUC'></legend><bdo id='phdUC'><pre id='phdUC'><center id='phdUC'></center></pre></bdo></b><th id='phdUC'></th></span></q></dt></tr></i><div id='phdUC'><tfoot id='phdUC'></tfoot><dl id='phdUC'><fieldset id='phdUC'></fieldset></dl></div>
                  2. <small id='phdUC'></small><noframes id='phdUC'>