I’m trying to make NSTable view app with adding rows with two columns x and y. I want x column to be constant string but y column I want increase the initial number by 1 every time I press button add.
Here is my TableController implementation code
@implementation TableViewController
-(id)init {
self = [super init];
if (self) {
list = [[NSMutableArray alloc]init];
}
return self;
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [list count];
}
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Number *p = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [p valueForKey:identifier];
}
-(IBAction)add:(id)sender{
[list addObject:[[Number alloc] init]];
[tableView reloadData];
}
-(void) dealloc {
[super dealloc];
}
@end
and Number implementation file:
@implementation Number
@synthesize x;
@synthesize y;
-(id) init {
self=[super init];
if (self) {
int j;
x = 5;
y=2+j;
j++;
}
return self;
}
@end
Number .h file:
#import <Foundation/Foundation.h>
int j;
@interface Number : NSObject {
@private
int x,y;
}
@property int x,y,j;
@end
But the number in y column doesn’t increase by 1 when I hit the add button. It seems to be reset every time I hit add button. Any help what am I doing wrong? Many thanks.
Declare j into .h file .
int .m file :