With the delegate and datasource connections made, I have the following controller:
#import <Foundation/Foundation.h>
@interface KextTable : NSObject <NSTableViewDataSource> {
@private
NSArray *klist;
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row;
@end
and
#import "KextTable.h"
@implementation KextTable
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
-(void) awakeFromNib
{
klist = [[NSArray alloc] init];
klist = [NSArray arrayWithObjects: @"1", @"2",
@"3", @"4", nil]; // debugging values only
}
- (void)dealloc
{
[super dealloc];
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
return [klist count];
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
return [klist objectAtIndex:row];
}
@end
And this code is crashing with EXC_BAD_ACCESS in my main interface control where the view is switched to the tab containing the table view. What is wrong?
(I know that connections are right, if I create the array in objectValueForTableColumn it works)
It’s because the klist you’re creating (the debug one) is using the constructor that autoreleases it. So you should add in:
Be sure to note that in what you’ve done there’s a memory leak (you create an NSArray and then re-assign the variable to something else…)