How to populate an NSTableView?

I am currently populating an NSTableView through a controller (MVC design pattern), where I initialize one NSMutableArray entry in the init controller method.

How would I:

  • Fill in my NSMutableArray, which is an array of Person objects
  • Should I populate the NSMutableArray in my mainViewDidLoad method of my base class? I did not find any examples or resources for this.

Model (Person.m)

 #import "Person.h" @implementation Person @synthesize name; @synthesize gender; - (id)init { self = [super init]; if (self) { name = @"Bob"; gender = @"Unknown"; } return self; } - (void)dealloc { self.name = nil; self.gender = nil; [super dealloc]; } @end 

Controller (PersonController.m)

 #import "PersonController.h" #import "Person.h" @implementation PersonController - (id)init { self = [super init]; if (self) { PersonList = [[NSMutableArray alloc] init]; // [personList addObject:[[Person alloc] init]]; // // [personTable reloadData]; } return self; } - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { return [personList count]; } - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { Person *person = [personList objectAtIndex:row]; NSString *identifier = [tableColumn identifier]; return [person valueForKey:identifier]; } - (void)dealloc { [super dealloc]; } @end 

Base file (Main.h):

 #import "Main.h" @implementation Main - (void)mainViewDidLoad { } @end 
+4
source share
3 answers

How would I:

  • Fill in my NSMutableArray, which is an array of Person objects

Step 1. Creating Person Objects.

Step 2: Add them to the array.

The commented code does just that, although you should probably create Person separately if you want to configure it (for example, set its name).

Should I populate the NSMutableArray in my mainViewDidLoad method of my base class?

It doesn't really matter how far before the user sees your model, you create it, but conceptually for me it smells a little. This has nothing to do with the view, so I say that it belongs to init .

Of course, if the main view and each of its views are already loaded, you need to tell the table to reload your data in order to display all the changes made to the array. Conversely, if you create a model before loading the view, you do not need to reboot initially, because the table view will already be prompted to specify your model once.

+3
source

Submitted, your commented code in PersonController looks right. I assume that NSTableRow has the correct identifier. The only problem is that your user object is empty, so there are no lines to display. I bet that what happened was that your line tried to display zero in the first line and instead displayed empty lines. Does your human object create, set the name and gender fields, and then put it in an NSMutableArray and call reloadData (basically what your code does with comments, except that now you provide some actual data to display)?

+1
source

Well, I suppose your controller has the IBTutView NSTableView * personTable property, which is bound in the interface builder?

In addition, a protocol must be declared in the controller interface, but again, since the implementation of the controller has the appropriate methods, I assume that you configured this correctly as well.

Another detail, are table column identifiers set correctly in the interface builder? From this example, I do not understand how the column identifiers relate to Person properties (name and gender). Should the personList array hold dictionary objects, where the dictionary object is human and the dictionary key is mapped to the column identifier that you specified in the interface builder?

Another technical characteristic, the property name (PersonList) should not start with a capital letter. I just think that the compiler should at least protest when you try to get personList using lowercase p.

0
source

Source: https://habr.com/ru/post/1394346/


All Articles