I have a custom NSTableView subclass which is bound to a data source (an NSArray) which updates asynchronously. When items are added to the array, rows are automatically added to the tableview. Awesome!
我有一个自定义的NSTableView子类,它绑定到一个异步更新的数据源(NSArray)。将项目添加到数组时,行会自动添加到tableview中。真棒!
My question is this: How can I detect that this magic has happened so that I can perform some other tasks related to the display of my custom tableview? Is there a method that I can override in my subclass which will be called when the tableview is updated?
我的问题是:如何检测到这种魔法已经发生,以便我可以执行与自定义tableview显示相关的其他一些任务?是否有一个方法可以在我的子类中覆盖,当tableview更新时将调用该方法?
2 个解决方案
#1
5
You don't need to subclass NSTableView to change its height based on the number of rows. In your controller, just monitor the data array using KVO and adjust the frame size of the tableview's scrollview (you can find it using enclosingScrollView
) when rows are added or removed. I've used this technique before and it's worked well. You can use the tableview's rowHeight
and intercellSpacing
methods to calculate the height of the frame.
您不需要子类化NSTableView来根据行数更改其高度。在您的控制器中,只需使用KVO监视数据数组,并在添加或删除行时调整tableview的scrollview的帧大小(您可以使用enclosingScrollView找到它)。我之前使用过这种技术,效果很好。您可以使用tableview的rowHeight和intercellSpacing方法来计算帧的高度。
#2
2
Looked high and low for days on this solution. It worked like a charm, thanks! Here's a sample of my code for others to follow:
在这个解决方案上看了几天的高低。它就像一个魅力,谢谢!以下是我的代码示例供其他人使用:
// tv = NSTableView
// view = NSView
int height = ([tv rowHeight] + [tv intercellSpacing].height) * [itemNodes count];
NSScrollView *sv = [tv enclosingScrollView];
NSRect svFrame = [sv frame];
svFrame.size.height = height;
[sv setFrame:svFrame];
NSRect viewFrame = [view frame];
viewFrame.size.height = height;
[view setFrame:viewFrame];
#1
5
You don't need to subclass NSTableView to change its height based on the number of rows. In your controller, just monitor the data array using KVO and adjust the frame size of the tableview's scrollview (you can find it using enclosingScrollView
) when rows are added or removed. I've used this technique before and it's worked well. You can use the tableview's rowHeight
and intercellSpacing
methods to calculate the height of the frame.
您不需要子类化NSTableView来根据行数更改其高度。在您的控制器中,只需使用KVO监视数据数组,并在添加或删除行时调整tableview的scrollview的帧大小(您可以使用enclosingScrollView找到它)。我之前使用过这种技术,效果很好。您可以使用tableview的rowHeight和intercellSpacing方法来计算帧的高度。
#2
2
Looked high and low for days on this solution. It worked like a charm, thanks! Here's a sample of my code for others to follow:
在这个解决方案上看了几天的高低。它就像一个魅力,谢谢!以下是我的代码示例供其他人使用:
// tv = NSTableView
// view = NSView
int height = ([tv rowHeight] + [tv intercellSpacing].height) * [itemNodes count];
NSScrollView *sv = [tv enclosingScrollView];
NSRect svFrame = [sv frame];
svFrame.size.height = height;
[sv setFrame:svFrame];
NSRect viewFrame = [view frame];
viewFrame.size.height = height;
[view setFrame:viewFrame];