以编程方式更新由Cocoa Bindings控制的TableView [英] Programmatically update a TableView that is governed by Cocoa Bindings

查看:107
本文介绍了以编程方式更新由Cocoa Bindings控制的TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对obj-c和可可很新,所以请与我一起:

I'm fairly new to obj-c and cocoa so please bear with me:

我有一个NSTableView设置了可可绑定,简单的-add -remove等方法由我的nib中的NSArrayController实例提供。我想以编程方式将对象添加到为此控制器(因此为表视图)提供内容的数组,然后相应地更新视图。

I have a NSTableView set up with cocoa bindings which works as expected with the simple -add -remove, etc methods provided by an instance of NSArrayController in my nib. I would like to programmatically add objects to the array that provides content for this controller (and hence for the table view) and then update the view accordingly.

我当前有一个工作方法添加一个新的对象到数组(由NSLog验证),但我不知道如何更新表视图。

I current have a working method for adding a new object to the array (verified by NSLog) but I can't figure out how to update the table view.

因此:我更新绑定的tableview?(即,我已经编程添加对象到我的数组)。 我实际上是在一些视图刷新代码像胶粘代码中的[查看reloadData],但我想要它与我已经到位的绑定。

So: How do I update the bound tableview? (ie, after I have programmatically added objects to my array). I'm essentially after some view refreshing code like [view reloadData] in glue code, but I want it to work with the bindings I have in place.

还是有这个问题的KVC / KVO相关解决方案?

Or is there a KVC/KVO related solution to this problem?

代码详细信息:
AppController.h

Code Details: AppController.h

@interface AppController : NSObject

@property NSMutableArray *clientsArray;

-(IBAction)addClientFooFooey:(id)sender;

@end

AppController.m init方法未在此处显示)

AppController.m (note, I also have the appropriate init method not shown here)

@implementation AppController
...

-(IBAction)addClientFooFooey:(id)sender{
    [self.clientsArray addObject:[[Client alloc] initWithFirstName: @"Foo" andLastName:@"Fooey"]];

//Need some code to update NSTableView here

}

@end

Client.h只是定义了两个属性:firstName和lastName。我的mainmenu.nib文件中的NSTableView的2列通过绑定到我的AppController实例的数组控制器适当地绑定到这些属性。

Client.h just simply defines two properties: firstName and lastName. The 2 columns of an NSTableView in my mainmenu.nib file are appropriately bound to these properties via an array controller bound to my AppController instance.

在旁注/替代。如何添加功能到现有的NSArrayController方法-add,例如:-addWithFirstName:andLastName,仍然与绑定兼容?

On a side note/as an alternative. How could I add functionality to the existing NSArrayController method -add, ie, something like: -addWithFirstName:andLastName and still have this compatible with bindings?

推荐答案

如果你的数组控制器绑定到 clientsArray ,你有两个主要的选项。

You have two main options for doing this provided your array controller is bound to clientsArray.

第一种方法是只使用数组控制器的 addObject:方法,而不是直接向 clientsArray

The first way is to just use the array controller's addObject: method instead of adding objects directly to clientsArray.

另一种方法是保持当前 addClientFooFooey:方法,但用以下两行包装您的现有代码:

The other way is to keep your current addClientFooFooey: method but wrap your existing code with these two lines:

[self willChangeValueForKey:@clientsArray];

[self didChangeValueForKey @clientsArray];

第一个选项是最直接的,但是如果你想要改变数组,由于某种原因,你需要更新数组,直接让KVO知道你在做。

The first option is the most straightforward, but if for some reason you need to update the array directly just let KVO know you are doing it.

这篇关于以编程方式更新由Cocoa Bindings控制的TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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