问题
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.
- iVar - > demoArray
- 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 - >
- Array Controller Bindings as show in below in the image
- 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