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

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

问题描述

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



自动调整NSTableView的大小



我想显示一个 NSWindow 中的NSTableView

现在,窗口的大小应该随着表视图调整。



就像Xcode一样:



<







这是相当简单的自动布局,你可以只固定内部视图到超级视图。



我的问题是,我不能找出最佳高度的表视图。
以下代码枚举了所有可用的行,但它不返回正确的值,因为表视图具有其他元素,如分隔符和表头。

   - (CGFloat)heightOfAllRows:(NSTableView *)tableView {
CGFloat __block height;
[tableView enumerateAvailableRowViewsUsingBlock:^(NSTableRowView * rowView,NSInteger row){
//试试这个
height + = rowView.frame.size.height;

//和这一个
// height + = [self tableView:nil heightOfRow:row];
}];

return height;
}






1。问题



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



2。问题



我应该在哪里运行这个代码?

我不想在控制器中实现这个,因为它肯定是表视图应该处理自身。

我甚至没有找到任何有用的委托方法。



所以我想最好是如果你可以子类 NSTableView

所以我的问题2,在哪里实现?





$ b b

动机



绝对值得赏金

解决方案

可以查询最后一行的框架以获得表视图的高度:

   - (CGFloat)heightOfTableView :( NSTableView *)tableView 
{
NSInteger rows = [self numberOfRowsInTableView:tableView];
if(rows == 0){
return 0;
} else {
return NSMaxY([tableView rectOfRow:rows - 1]);
}
}

这假设一个没有边框的封闭滚动视图! / p>

您可以查询 tableView.enclosingScrollView.borderType 以检查滚动视图是否有边框。如果是,则需要将边框宽度添加到结果中(两次;底部和顶部)。不幸的是,我不知道我的头顶如何获得边框宽度。



查询 rectOfRow:是它在与 [tableView reloadData]; 相同的runloop迭代中工作。根据我的经验,当您先执行 reloadData (您将获得之前的高度)时,查询表视图的框架不会可靠地工作。


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:



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

解决方案

You can query the frame of the last row to get the table view's height:

- (CGFloat)heightOfTableView:(NSTableView *)tableView
{
    NSInteger rows = [self numberOfRowsInTableView:tableView];
    if ( rows == 0 ) {
        return 0;
    } else {
        return NSMaxY( [tableView rectOfRow:rows - 1] );
    }
}

This assumes an enclosing scroll view with no borders!

You can query the tableView.enclosingScrollView.borderType to check whether the scroll view is bordered or not. If it is, the border width needs to be added to the result (two times; bottom and top). Unfortunately, I don't know of the top of my head how to get the border width.

The advantage of querying rectOfRow: is that it works in the same runloop iteration as a [tableView reloadData];. In my experience, querying the table view's frame does not work reliably when you do a reloadData first (you'll get the previous height).

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

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