gpt4 book ai didi

macos - 为什么 NSTableView 可能会重绘滚动时的每个单元格?

转载 作者:行者123 更新时间:2023-12-03 16:08:04 27 4
gpt4 key购买 nike

我有一个包含 5 列的 NSTableView,每列的 Nib 中都包含一个库存 NSTableCellView。 (库存单元格有一个文本框和一个可选图像。)填充后,该表大约有 50 行。一切都显示良好,但滚动性能非常糟糕。看起来这种情况正在发生,因为每当表格滚动时,每个单元格都会收到其完整矩形的 drawRect: 消息。但是, reloadData 和 reloadDataForRowIndexes:ColumnIndexes: 都没有被调用,所以事实并非如此。这也不是单元格的内容:我尝试注释掉所有代码,只保留每个单元格的默认单元格图像和文本,并且性能是相同的。滚动时,没有任何单元格得到更新。 (我在 tableView:viewForTableColumn:row: 中放置了一个断点以确保。)

我的实现具有以下委托(delegate)方法:

  • tableView:viewForTableColumn:row: 在委托(delegate)中;这通过 makeViewWithIdentifier:owner: 创建并填充新单元格:
  • numberOfRowsInTableView:在数据源中;这将返回一个常量
  • tableView:sortDescriptorsDidChange: 在数据源中

就是这样!还不是很复杂。

我觉得我错过了一些完全明显的东西。是什么导致了这些重绘?

<小时/>

编辑:想想看,其他几个应用程序(uTorrent、Xcode)似乎也表现出同样的缓慢滚动行为。如果您在滚动时查看 CPU 使用情况,您确实可以看到这一点。另一方面,事件监视器的滚动非常流畅,几乎不会对 CPU 造成任何影响。如何在我的应用程序中获取它?

<小时/>

编辑2:我想我发现了我的错误。 According to Apple :

In iOS apps, Core Animation is always enabled and every view is backed by a layer. In OS X, apps must explicitly enable Core Animation support by doing the following:

  • Link against the QuartzCore framework. (iOS apps must link against this framework only if they use Core Animation interfaces explicitly.)
  • Enable layer support for one or more of your NSView objects by doing one of the following:

    • In your nib files, use the View Effects inspector to enable layer support for your views. The inspector displays checkboxes for the selected view and its subviews. It is recommended that you enable layer support in the content view of your window whenever possible.
    • For views you create programmatically, call the view’s setWantsLayer: method and pass a value of YES to indicate that the view should use layers.

Enabling layer support in one of the preceding ways creates a layer-backed view. With a layer-backed view, the system takes responsibility for creating the underlying layer object and for keeping that layer updated. In OS X, it is also possible to create a layer-hosting view, whereby your app actually creates and manages the underlying layer object. (You cannot create layer-hosting views in iOS.) For more information on how to create a layer-hosting view, see “Layer Hosting Lets You Change the Layer Object in OS X.”

一旦解决了性能问题,我就会添加答案。粗略地通过,我的滚动仍然很颠簸,但滚动时我的 CPU 使用率从 70% 下降到 10%。

最佳答案

郑重声明...OP 的编辑 2 使世界变得不同。

In iOS apps, Core Animation is always enabled and every view is backed by a layer. In OS X, apps must explicitly enable Core Animation support by doing the following:

Link against the QuartzCore framework. (iOS apps must link against this framework only if they use Core Animation interfaces explicitly.) Enable layer support for one or more of your NSView objects by doing one of the following:

In your nib files, use the View Effects inspector to enable layer support for your views. The inspector displays checkboxes for the selected view and its subviews. It is recommended that you enable layer support in the content view of your window whenever possible. For views you create programmatically, call the view’s setWantsLayer: method and pass a value of YES to indicate that the view should use layers. Enabling layer support in one of the preceding ways creates a layer-backed view. With a layer-backed view, the system takes responsibility for creating the underlying layer object and for keeping that layer updated. In OS X, it is also possible to create a layer-hosting view, whereby your app actually creates and manages the underlying layer object. (You cannot create layer-hosting views in iOS.) For more information on how to create a layer-hosting view, see “Layer Hosting Lets You Change the Layer Object in OS X.”

关于macos - 为什么 NSTableView 可能会重绘滚动时的每个单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19962721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com