• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: DataSource class of NSTableView - weird
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: DataSource class of NSTableView - weird


  • Subject: Re: DataSource class of NSTableView - weird
  • From: "M. Uli Kusterer" <email@hidden>
  • Date: Mon, 10 Nov 2003 20:53:40 +0100

At 19:19 Uhr +0100 10.11.2003, Kai wrote:
> Maybe it would help if you tried to create a small (i.e. minimal)
sample application that reproduces your problem and posted the code?
Ok, here is a simple small test app (made with Xcode 1.0.1), that shows
the problem. Hope, someone has a solution!

First, you forgot to initialize databaseTables to nil. It may contain any value, even garbage, which means your !databaseTables test will just fail and you find yourself working with an uninitialized pointer variable that points to an arbitrary memory location instead of to an array.

Second, you are creating *two* instances of your data source. You have one in your NIB file (the blue box), and one that you're creating inside TestApp's awakeFromNib method. Your TestApp will use the one it created, which isn't hooked up to the table view, and then IB will create a second one, which is hooked up to the table view.

What you probably intended to do, was create only one. There are two options:

1) Delete the blue box "MyDataSource" from the NIB file. However, then you'll have to manually set up the table view's data source and MyDataSource's TableView outlet.

2) Turn the MyDataSource* in TestApp into an IBOutlet and remove the code that creates a MyDataSource in TestApp.awakeFromNib. Then you can hook up the outlet of TestApp to the blue box "MyDataSource" in your NIB and you'll have only a single object, as it should be.

PS - I suppose you have a valid reason for having separate objects of MyDataSource sharing the same array. Typically, each data source object would have its own array, and you'd share the data source between different table views, or whatever.

Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de

TestApp.h:
//
// TestApp.h
// TestApp
//

#import <Foundation/Foundation.h>
#import "MyDataSource.h"

@interface TestApp : NSObject {
MyDataSource *datasource;
}

- (IBAction)Button_clicked:(id)sender;

@end

TestApp.m:
//
// TestApp.m
// TestApp
//

#import "TestApp.h"

@implementation TestApp

- (void)awakeFromNib {
datasource = [[MyDataSource alloc] init];
}

- (IBAction)Button_clicked:(id)sender {
NSMutableArray *tables_array = [[NSMutableArray alloc] init];

[tables_array addObject:@"Entry 1"];
[tables_array addObject:@"Entry 2"];
[tables_array addObject:@"Entry 3"];
[tables_array addObject:@"Entry 4"];
[tables_array addObject:@"Entry 5"];

[datasource fillTableList:tables_array];
}

@end

MyDataSource.h:
/* MyDataSource */

#import <Cocoa/Cocoa.h>

NSMutableArray *databaseTables;

@interface MyDataSource : NSObject {
IBOutlet NSTableView *TableList;
}

- (void)fillTableList:(NSArray *)currentTables;

@end

MyDataSource.m:
#import "MyDataSource.h"

@implementation MyDataSource

- (void)awakeFromNib {
if ( !databaseTables )
databaseTables = [[NSMutableArray alloc] init];

/*
// With this, table view was filled, but the GUI will be not updated correctly !
// Without this, nothing happens !
NSMutableArray *tables_array = [[NSMutableArray alloc] init];

[tables_array addObject:@"New entry 1"];
[tables_array addObject:@"New entry 2"];
[tables_array addObject:@"New entry 3"];

[self fillTableList:tables_array];
*/
}

- (void)fillTableList:(NSArray *)currentTables {
[databaseTables removeAllObjects];
[databaseTables addObjectsFromArray:currentTables];

[TableList reloadData];
}

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

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
return [databaseTables objectAtIndex:row];
}

@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: DataSource class of NSTableView - weird
      • From: Kai <email@hidden>
References: 
 >DataSource class of NSTableView - weird (From: Kai <email@hidden>)
 >Re: DataSource class of NSTableView - weird (From: "M. Uli Kusterer" <email@hidden>)
 >Re: DataSource class of NSTableView - weird (From: Kai <email@hidden>)
 >Re: DataSource class of NSTableView - weird (From: "M. Uli Kusterer" <email@hidden>)
 >Re: DataSource class of NSTableView - weird (From: Kai <email@hidden>)

  • Prev by Date: cocoa help
  • Next by Date: Re: Updating the main menu
  • Previous by thread: Re: DataSource class of NSTableView - weird
  • Next by thread: Re: DataSource class of NSTableView - weird
  • Index(es):
    • Date
    • Thread