NSTableView中的reloadData但保留当前选择 [英] reloadData in NSTableView but keep current selection

查看:207
本文介绍了NSTableView中的reloadData但保留当前选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTableView,它显示目录的内容.我监视FSEvents,并且每次收到事件时,都会重新加载表视图. 不幸的是,当前选择然后消失了.有办法避免这种情况吗?

I have anNSTableView showing the contents of a directory. I watch for FSEvents, and each time I get an event I reload my table view. Unfortunately, the current selection then disappears. Is there a way to avoid that?

推荐答案

这取决于您填充NSTableView的方式.

It depends on how you populate your NSTableView.

如果将表视图绑定到NSArrayController,而该表视图又包含表视图显示的项目,则NSArrayController可以保留选择内容.您可以从Interface Builder中选择(或不选择)它作为NSArrayController上的一个属性.或者,您可以在代码中使用setPreservesSelection方法.

If you have the table view bound to an NSArrayController, which in turn contain the items that your table view is displaying, then the NSArrayController has an option to preserve the selection. You can select it (or not) from within Interface Builder as a property on the NSArrayController. Or you can use the setPreservesSelection method in code.

但是,如果每次获得FSEvent时都完全替换了NSArrayController管理的项目数组,那么选择的保留可能无法正常工作.不幸的是,苹果公司关于NSArrayController的此属性的文档在何时可以保留和不能保留所选内容方面有些含糊.

However, if you completely replace the array of items that the NSArrayController manages each time you get your FSEvents, then maybe the preservation of selection cannot work. Unfortunately the Apple docs on this property of NSArrayController are a bit vague as to when it can and cannot preserve the selection.

如果您不使用NSArrayController,而是使用数据源填充表视图,那么我认为您必须自己管理选择.

If you are not using an NSArrayController, but maybe using a dataSource to populate the table view, then I think you'll have to manage the selection yourself.

这篇关于NSTableView中的reloadData但保留当前选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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