将NSTableView绑定到NSArrayController,而不是直接从模型数组中获取数据 [英] Binding an NSTableView to an NSArrayController instead of fetching the data directly from my model array
问题描述
当前情况:直接绑定
我有一个 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
-
在插入内容时,在阵列控制器上调用
-add:
。 -
如果您使用不可变数组,您可以将 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:
Call
-add:
on the array controller when you insert content. That probably that won't help in your case.If you use immutable arrays you could bind the array controller to
myModel
'scontactsArray
key instead of assigning it.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屋!