Hi all,

I try to create a very simple list of persons that will be displayed in a NSTableView. I have a few options like: save the list using NSArchive, load a list from the disk, adding, removing entries.
The data source is a NSObject subclass which implements:

-(int) numberOfRowsInTableView:(NSTableView*) tableView
-(id) tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *) tableColumn
row:(int) row

Also the datasource conforms to NSCoding protocol.
Now the problem:
I can save the data on the disc using

-(void) encodeWithCoder:(NSCoder*) encoder;

but when I try to load the data from the disk in
-(id) initWithCoder:(NSCoder*) encoder I see the NSMutableArray loaded with all data but in -(int) numberOfRowsInTableView:(NSTableView*) tableView the array is emtpy and I cannot see nothing in my NSTableView.

I know that's a very simple problem but I m new in Objective C (I migrate from C++/Symbian) so..please help.
I put the code if you want to take a look.


@interface DataController :  NSObject <NSCoding> {
    IBOutlet NSTableView *tblTableView;
    NSMutableArray *persons;
}

-(void) addNewPersonWithFirstName:(NSString *) firstName andLastName: (NSString*) lastName andPhoneNumber:(NSString *) phoneNumber;

-(id) initWithCoder:(NSCoder*) encoder;
-(void) encodeWithCoder:(NSCoder*) encoder;
@end


@implementation DataController

-(id) init
{
  self=[super init];
  persons=[[NSMutableArray alloc] init];
  return self;
}

-(id) initWithCoder:(NSCoder*) encoder
{
    self=[super init];
  NSMutableArray *aux=[encoder decodeObject];
 [aux retain];
   [persons release];
  persons=aux;
    return self;
}

-(void) encodeWithCoder:(NSCoder *) encoder
{
   [encoder encodeObject:persons];
}

-(int) numberOfRowsInTableView:(NSTableView*) tableView
{
    return [persons count];
}

-(id) tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *) tableColumn
row:(int) row
{
   NSString *identifier=[tableColumn identifier];
  Person *p=[persons objectAtIndex:row];
  return [p valueForKey:identifier];
}

-(void) addNewPersonWithFirstName:(NSString *) firstName andLastName: (NSString *) lastName andPhoneNumber:(NSString *) phoneNumber
{
  Person *p=[[Person alloc] init];
    p.firstName=firstName;
  p.lastName=lastName;
    p.phoneNumber=phoneNumber;
  [persons addObject:p];
  [p release];
    [tblTableView reloadData];
  [tblTableView selectRow:0 byExtendingSelection:NO];
}

-(void) dealloc
{
    [persons release];
  [super dealloc];
}

@end

=
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to