How to bind the NSMutableArray to ArrayController through xib

隐身守侯 提交于 2019-11-30 19:25:39

问题


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.


回答1:


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.




回答2:


The steps are as follows (just in case you missed anything):

  • Add the array controller
  • Set the Mode of the array controller to Class and add the Class name.
  • Add the Keys you want to bind

If you are using and View based table view. Do following:

  • Click the column in your table view (often takes three clicks on the table view)
  • Check Value in Bindings Inspector
  • Controller Key: ArrangedObjects and your Model Key Path.
  • Mark your Static Text in your table view cell.
  • Bind your Static Text to Table Cell View and your Model Key Path.

Hope it helps!



来源:https://stackoverflow.com/questions/18457346/how-to-bind-the-nsmutablearray-to-arraycontroller-through-xib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!