NSTableViewDataSource 还是 NSArrayController?
我需要在用户滚动 NSTableView 时动态加载数据。例如,该表可能显示 50 行,随着它进一步滚动,我需要从网络获取更多数据。对象/行的数量是事先已知的,因此我希望表格从一开始就有正确的行数,但在加载数据时显示空单元格。
我正在使用 Core Data,因此可以轻松使用绑定将表连接到我的模型。这也将负责在数据传入和解析时更新单元格。我试图弄清楚如何通过子类化 NSArrayController
来做到这一点,但据我所知,没有任何信息从表流到控制器来了解哪些行实际需要数据。因此,我正在考虑实现 NSTableViewDataSource
,这样我可以轻松检查表格是否已滚动到可用数据的行之外。另一方面,我不知道使用此解决方案是否可以轻松自动更新单元格。
I need to load data dynamically as a user scrolls through an NSTableView
. For example, the table might display 50 rows, and as it scrolls further I need to fetch more data from the network. The number of of objects/rows is known beforehand, so I want the table to have the right number of rows from the start, but showing empty cells while data is loading.
I'm using Core Data so it's easy to connect the table to my model using bindings. This would also take care of cells being updated as data comes in and is parsed. I've tried to figure out how I could do this by subclassing NSArrayController
but from what I can tell there is no information flowing from the table to the controller about which rows actually need data. Therefore, I'm thinking of implementing NSTableViewDataSource
instead, where I can easily check if the table has scrolled beyond the rows for which data is available. On the other hand, I don't know if I will get cells automatically updating as easily with this solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人遇到这个问题,这是我自己的答案:
是,您需要在控制器上实现 NSTableViewDataSource,观察数据的变化并在表上手动调用
reloadData
发生变化。这样做的主要原因是您可以推迟数据的加载,直到实际需要时(当表视图滚动时)。使用数据源协议可以让您了解所请求的索引。In case anyone comes across this, here's my own answer:
Yes, you need to implement NSTableViewDataSource on a controller, observe changes on the data and call
reloadData
manually on the table when changes occur. The main reason for doing this is that you can defer loading of data until it's actually needed (when the table view scrolls). Using the data source protocol keeps you informed of which indexes are requested.