How to bind the NSMutableArray to ArrayController through xib

假如想象 提交于 2019-12-01 01:01:52

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.

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!

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