Automatically adjust size of NSTableView

蓝咒 提交于 2019-12-04 12:49:04

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

EDIT:

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
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!