自动调整NSTableView的大小 [英] Automatically adjust size of NSTableView

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

问题描述

我有一个NSTableView,它应该在添加行时调整框架的大小。
我将创建2个属性,maxHeight和minHeight,如果任何行被添加或删除,我将调整表视图的大小以适合它的内容,如果它没有超过限制。

I have an NSTableView, which should resize it's frame when a row is added. I would make 2 properties, maxHeight and minHeight, and if any rows are being added or removed, I would resize the table view to fit it's content, if it doesn't cross the limit.

像Safari下载面板(10.7或更高版本)。
有人有想法如何做到这一点?
我想在一个子类中处理这个。所以不要在委托类中调整大小。

Like the Safari Download Panel (10.7 or later). Has anyone an idea how to do this? I would want to handle this in a subclass. So no messing with the resizing in the delegate class.

我至少需要知道当表视图被重新加载时被调用的方法。 reloadTable只调用真正的重载方法,所以没有成功。

I would at least need to know which method is being called when the table view is being reloaded. reloadTable only invokes the real reloading method, so no success there.

推荐答案

控制器,而不是一个子类(对不起,如果它不是你要找的)。基本上我写了一个方法,通过添加所有行的高度来计算tableview的高度。每次我从表中添加或删除一行时,我会调用该方法。这是让你开始的事情:

I did something similar a while back and I did it on the controller, not on a subclass (sorry if it's not what you're looking for). Basically I wrote a method that computed the height of the tableview by adding the height of all the rows. And every time I added or removed a row from the table I'd call that method. Here is something to get you started:

- (void)adjustTableSize
{
    NSInteger minHeight = ...
    NSInteger maxHeight = ...

    NSInteger tViewHeight = 0;
    for (int i = 0; i < [tableView numberOfRows]; i++) {
        NSView* v = [tableView viewAtColumn: 0 row: i makeIfNecessary: YES]; // Note that this is for view-based tableviews
        tViewHeight += v.frame.size.height;
    }

    NSInteger result = MIN(MAX(tViewHeight, minHeight), maxHeight);

    // Do something with result here
}

你希望它在一个子类应该可能,但它可能是一个痛苦的工作如何...

If you really want it on a subclass it should possible, but it might be a pain to work out how...

如果您不介意使用未收集的API,请使用以下更简单的版本:

If you don't mind working with undocummented APIs, here's a simpler version:

- (void)adjustTableSize
{
    NSInteger minHeight = ...
    NSInteger maxHeight = ...
    NSInteger result = MIN(MAX([tableView _minimumFrameSize].height, minHeight), maxHeight);

    // Do something with result here
}

这是undocummented我不能承诺它会工作,但从我的测试,到目前为止。 可能比创建视图更快地获取其高度,特别是如果您有很多行。

Since this is undocummented I can't promise it'll work, but from my testing so far it does. And it might be faster than creating views just to get their height, specially if you have lots of rows.

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

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