如何更新NSTableView从这个可变数组在Cocoa工作? [英] How does updating NSTableView from this mutable array work in Cocoa?

查看:204
本文介绍了如何更新NSTableView从这个可变数组在Cocoa工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个NSTableView应该包含文件列表。我有一个按钮,用于打开一个对话框,并以编程方式添加文件到此列表。有一段时间,我在添加文件时无法更新表视图,因为我使用了以下代码:

In my application, I have an NSTableView which should contain a list of files. I have a button that is used to open an dialog and programmatically add files to this list. For some time, I could not get the table view to update when I was adding files, as I was using the following code:

[self.newPackage.files addObject:fileURL];

现在这对我来说很有意义。根据我的理解,上面的代码行将改变可变数组后面的控制器的背后。

It makes sense to me now that this doesn't work. As I understand it, the above line of code would be changing the mutable array "behind the controller's back."

我能够拼凑一个工作的解决方案,大部分从此问题 a>,使用以下代码:

I was able to piece together a working solution, largely from this question, with the following code:

NSMutableArray *bindingsCompliantArray = [[self valueForKey:@"newPackage"] mutableArrayValueForKey:@"files"];
[bindingsCompliantArray addObject:fileURL];

但是,我不明白这是如何工作的。 bindingsCompliantArray也不在任何地方使用。我查看了文档mutableArrayValueForKey ,但它不会使它更清晰。有没有人可以帮助解释这是如何工作的?

However, I don't understand how this works. bindingsCompliantArray is not used anywhere else either. I've looked at the documentation for mutableArrayValueForKey, but it's not making it any clearer. Is there anyone that could help explain how this is working?

推荐答案

-mutableArrayValueForKey:方法返回一个代理数组,你可以把它看作是原始数组,额外的好处是任何KVO观察者观察数组观察到数组的变化。

The ‑mutableArrayValueForKey: method returns a proxy array that you can treat as if it's the original array, with the added bonus that changes to the array are observed by any KVO observers watching the array.

NSController 子类,例如 NSArrayController

当您通过此方法接收代理数组时, NSMutableArray 方法例如 -addObject:将被观察者注意到,而使用标准数组则不是这样。

When you receive a proxy array via this method, NSMutableArray methods such as ‑addObject: will be noticed by observers, whereas with a standard array this is not the case.

这篇关于如何更新NSTableView从这个可变数组在Cocoa工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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