gpt4 book ai didi

objective-c - 将 NSTableView 绑定(bind)到 NSArrayController,而不是直接从模型数组中获取数据

转载 作者:行者123 更新时间:2023-12-03 17:42:19 25 4
gpt4 key购买 nike

当前情况:直接绑定(bind)

我有一个 NSTableView,它显示来自自定义对象的 NSArray 的数据。

所需的方法:数组 Controller

但是,使用 NSArrayController 作为我的任意模型对象的外观将是“更好的实践”。

因此,我在应用程序中复制了该表,并创建了一个新的 NSViewController,其中将 NSTableViewTable Content 绑定(bind)到 >NSArrayController。我为此类添加了一个 IBOutlet,因此我现在可以像这样设置我的 View Controller :

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

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

现在,由于我将 TableView 的表内容绑定(bind)到数组 Controller ,我希望一旦我用数据(来自远程服务)填充数组, TableView 就会已更新,这是我直接绑定(bind)到 myModel.contactsArray 的其他方法的情况。

问题:没有数据

但是,该表仍然是空的。我是否必须设置其他东西才能使其工作(也许是一些 willChangeValueForKey-magic 或其他我错过的东西。我已经尝试将数组 Controller 设置为 dataSource 并且NSTableViewdelegate 但这似乎也不起作用(无论如何,我不清楚 dataSourceTable 如何工作内容绑定(bind)“彼此相关”)。

最佳答案

当你用内容填充数组时,你是重新分配一个不可变数组还是附加到一个可变数组?不管怎样,它都不会像你期望的那样工作。

当您以这种方式设置数组 Controller 的内容时​​,数组 Controller 只是复制内容,而不是保留引用。无论它是可变数组(还是重新分配模型的contactsArray),后续更改都不会产生任何影响。

相反:

  1. 插入内容时,在数组 Controller 上调用-add:。这可能对您的情况没有帮助。

  2. 如果您使用不可变数组,您可以将数组 Controller 绑定(bind)myModelcontactsArray键,而不是分配它。

  3. 如果您使用可变数组,我认为这不起作用。您可以在模型中创建第二个数组 Controller ,并将 View 的数组 Controller 绑定(bind)到 contactsArrayController.arrangedObjects。也许有更好的方法,但这就是我过去所做的。

关于objective-c - 将 NSTableView 绑定(bind)到 NSArrayController,而不是直接从模型数组中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9164867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com