将NSTableView绑定到NSArrayController,而不是直接从模型数组中获取数据 [英] Binding an NSTableView to an NSArrayController instead of fetching the data directly from my model array

查看:529
本文介绍了将NSTableView绑定到NSArrayController,而不是直接从模型数组中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前情况:直接绑定



我有一个 NSTableView ,显示来自<$



所需方法:数组控制器



但是,使用 NSArrayController 作为我的arbritary模型对象的外观是更好的做法。



所以我复制了我的应用程序中的表,并创建了一个新的 NSViewController ,其中绑定了表内容 NSTableView NSArrayController 。我为这个类添加了一个 IBOutlet ,现在我可以这样设置我的View Controller:

  ContactListViewController * myVC = [[ContactListViewController alloc] init]; 

//告诉ArrayController内容数组所在的位置。
myVC.contactsArrayController.content = self.myModel.contactsArray;

现在由于我绑定了表内容表视图到数组控制器,我希望,只要我用数据(从远程服务)填充数据表更新,这是我的其他方法的情况下,我绑定到 myModel.contactsArray



问题:没有数据

,表保持为空。我必须设置一些其他的工作(也许一些 willChangeValueForKey -magic或其他我错过了我已经尝试设置阵列控制器为 dataSource NSTableView 委托 (无论如何不清楚我如何 dataSource 表内容绑定彼此相关)。

解决方案

当您使用内容填充数组时,是重新分配一个不可变数组还是附加到一个可变数组?



当你以这种方式设置数组控制器的内容,而不是保持一个引用,数组控制器



而不是:

$ b(只是复制内容),后续的更改将无效,无论它是一个可变数组(也不重新分配模型的contactsArray)
$ b


  1. 在插入内容时,在阵列控制器上调用 -add:


  2. 如果您使用不可变数组,您可以 c $ c> myModel '

    p>如果你使用一个可变数组,我不认为这将工作。您可以在模型中创建第二个数组控制器,并将视图的数组控制器绑定到 contactsArrayController.arrangedObjects



Current Situation: Direct Binding

I have an NSTableView which displays the data that comes from an NSArray of my custom objects.

Desired Approach: Array Controller

However, It would be "more best practise" to use an NSArrayController instead as a facade to my arbritary model object.

So I duplicated the table in my application and created a new NSViewController where I bound the Table Content of the NSTableView to the NSArrayController. I added an IBOutlet for this class so I can now setup my View Controller like this:

ContactListViewController *myVC = [[ContactListViewController alloc] init];

// Tell the ArrayController where the content-array is.
myVC.contactsArrayController.content = self.myModel.contactsArray;

Now since I bound the Table Content of the table view to the array controller, I would expect, that as soon as I fill the array with data (from a remote service) that the tableview is updated, which is the case for my other approach where I bind to myModel.contactsArray directly.

The Problem: No data

However, the table remains empty. Do I have to setup something else for this to work (Maybe some willChangeValueForKey-magic or something else I have missed. I already tried to set the array controller as the dataSource and the delegate of the NSTableView but that did not seem to work either (Anyway it is not clear to me how dataSource and the Table Content binding "relate to each other").

解决方案

When you fill up your array with content, are you re-assigning an immutable array or appending to a mutable array? Either way, it won't work quite how you're expecting.

When you set the content of an array controller this way, instead of keeping a reference, the array controller just copies the content. Subsequent changes will have no effect, whether it's a mutable array (nor if you reassign the model's contactsArray).

Instead:

  1. Call -add:on the array controller when you insert content. That probably that won't help in your case.

  2. If you use immutable arrays you could bind the array controller to myModel's contactsArray key instead of assigning it.

  3. If you use a mutable array I don't think that will work. You could create a second array controller in the model and bound the view's array controller to contactsArrayController.arrangedObjects. Maybe there's a better way, but that's what I've done in the past.

这篇关于将NSTableView绑定到NSArrayController,而不是直接从模型数组中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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