如何通过xib将NSMutableArray绑定到ArrayController [英] How to bind the NSMutableArray to ArrayController through xib

查看:194
本文介绍了如何通过xib将NSMutableArray绑定到ArrayController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cocoa的新人。我显示一个简单的Tableview填充与NSMutableArray绑定到NSArrayController如下

  [_ arrController bind:@contentArraytoObject :self withKeyPath:@dataArrayoptions:nil]; 

这里_arrController是我的NSArrayController的IBoutlet,dataArray是我的NSmutableArray与数据。



当我以编程方式进行绑定时,我可以成功填充Tableview。但我不能通过Interface Builder实现相同的绑定。



我在我的IB中选择ArrayController,去绑定部分,并尝试在控制器部分绑定选择模型键路径作为dataArray.But但是,我的表没有填充数据,其中作为编程我可以轻松实现我的任务。

解决方案

这里有一个例子,可能会帮助你解决这个问题。


  1. iVar - > demoArray

  2. IBOutlet - > demoArrayController




@interface ExAppDelegate:NSObject



@property(assign)IBOutlet NSWindow * window;



@property(strong)NSMutableArray * demoArray;



@property(strong)IBOutlet NSArrayController * demoArrayController;



@end


下面是同一个类的实现

  @implementation ExAppDelegate 
@synthesize demoArray;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//在这里插入代码来初始化应用程序
self.demoArray = [NSMutableArray array];
for(int i = 0; i <= 10; i ++)
{
NSMutableDictionary * temp = [NSMutableDictionary dictionary];
[temp setObject:[NSNumber numberWithInt:i] forKey:@number];
[temp setObject:[NSString stringWithFormat:@Demo%d,i] forKey:@demoKey];
[self.demoArray addObject:temp];
}
[self.demoArrayController rearrangeObjects];
}

@end

现在,UI Bindings - >


  1. 数组控制器绑定如下图所示

>


  1. 表视图列绑定。



注意



1。在将对象添加到绑定到数组控制器的数组后,请确保您在数组控制器上调用了rearrangeObjects。

[self.demoArrayController rearrangeObjects] ;



2。在表格视图中,列绑定确保您选中了连续更新值复选框。



我希望这可以解决问题。


I am new to Cocoa . I am displaying a simple Tableview populated with NSMutableArray which is bound to the NSArrayController as follows

[_arrController bind:@"contentArray" toObject:self withKeyPath:@"dataArray" options:nil];

Here _arrController is the IBoutlet to my NSArrayController and dataArray is my NSmutableArray with the data.

I am successful in populating the Tableview when I do the binding programatically. However I am not able to achieve the same binding through the Interface Builder.

I selected ArrayController in my IB , went to binding section,and tried binding under the controller section by selecting the model key path as dataArray.But however , my table is not populated with data , where as programatically I can achieve my task easily. Any help is appreciated.

解决方案

Here is a example which might help you to fix the issue.

  1. iVar - > demoArray
  2. IBOutlet -> demoArrayController

@interface ExAppDelegate : NSObject

@property (assign) IBOutlet NSWindow *window;

@property (strong) NSMutableArray *demoArray;

@property (strong) IBOutlet NSArrayController *demoArrayController;

@end

Below is the implementation of the same class

@implementation ExAppDelegate
@synthesize demoArray;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    self.demoArray = [NSMutableArray array];
    for (int i=0 ; i <= 10 ; i++)
    {
        NSMutableDictionary *temp = [NSMutableDictionary dictionary];
        [temp setObject:[NSNumber numberWithInt:i] forKey:@"number"];
        [temp setObject:[NSString stringWithFormat:@"Demo %d",i] forKey:@"demoKey"];
        [self.demoArray addObject:temp];        
    }
    [self.demoArrayController rearrangeObjects];
}

@end

Now, UI Bindings - >

  1. Array Controller Bindings as show in below in the image

  1. Table View Column bindings.

NOTE:

1. Make sure you are calling the rearrangeObjects on Array Controller after you add the objects to an array which is binded to Array Controller.

[self.demoArrayController rearrangeObjects];

2. In Table View Column Bindings make sure you have checked the check box "Continuously Updates Value".

I Hope this fix the issue.

这篇关于如何通过xib将NSMutableArray绑定到ArrayController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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