2012-07-05 109 views
0

我想使NSTable視圖應用程序添加具有兩列x和y的行。我希望x列是常量字符串,但是y列我想每次按下按鈕時都將初始數字加1。NSTableView在行中添加數量增加

這裏是我的TableController實現代碼

@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 

和數量實現文件:

@implementation Number 

@synthesize x; 
@synthesize y; 


-(id) init { 
self=[super init]; 
if (self) { 
    int j; 
    x = 5; 
    y=2+j; 
    j++; 
} 

return self; 
} 

@end 

數.h文件中:

#import <Foundation/Foundation.h> 
int j; 
@interface Number : NSObject { 
@private 
int x,y; 
} 

@property int x,y,j; 

@end 

但y中列數不當我點擊添加按鈕時增加1。這似乎是每次我點擊添加按鈕時重置。任何幫助我做錯了什麼?非常感謝。

回答

0

將j聲明爲.h文件。

.f file: 
    int j; 

INT .m文件:

-(id) init { 
self=[super init]; 
if (self) { 
    x = 5; 
    y=2+j; 
    j++; 
} 

return self; 
} 
+0

感謝這似乎工作。 – ste4lth 2012-07-05 10:19:53