2012-06-23 77 views
0

我对Cocoa相当陌生,我试图设置一个由数组支持的表视图。我已经设置了应用程序委托作为tableview的数据源,并实现了NSTableViewDataSource协议。初始化NSTableView

当我运行应用程序,我得到以下日志输出:

2012-06-23 18:25:17.312 HelloWorldDesktop[315:903] to do list is nil
2012-06-23 18:25:17.314 HelloWorldDesktop[315:903] Number of rows is 0
2012-06-23 18:25:17.427 HelloWorldDesktop[315:903] App did finish launching

我认为,当我在的tableView称为reloadData将再次numberOfRowsInTableView:(NSTableView *)tableView打电话刷新视图,但似乎并不正在发生。我错过了什么?

我的.h和.m列表如下。

AppDelegate.h

#import <Cocoa/Cocoa.h> 

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource> 

@property (assign) IBOutlet NSWindow *window; 
@property (assign) IBOutlet NSTableView * toDoListTableView; 

@property (assign) NSArray * toDoList; 

@end 

AppDelegate.m

#import "AppDelegate.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize toDoList; 
@synthesize toDoListTableView; 

- (void)dealloc 
{ 
    [self.toDoList dealloc]; 
    [super dealloc]; 
} 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSLog(@"App did finish launching"); 
    // Insert code here to initialize your application 
    // toDoList = [[NSMutableArray alloc] init]; 
    toDoList = [[NSMutableArray alloc] initWithObjects:@"item 1", @"item 2", nil]; 
    [self.toDoListTableView reloadData]; 
    // NSLog(@"table view %@", self.toDoListTableView); 

} 

//check toDoList initialised before we try and return the size 
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView { 
    NSInteger count = 0; 
    if(self.toDoList){ 
     count = [toDoList count]; 
    } else{ 
     NSLog(@"to do list is nil"); 
    } 
    NSLog(@"Number of rows is %ld", count); 
    return count; 
} 

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { 
    NSLog(@"in objectValueForTable"); 
    id returnVal = nil; 

    NSString * colId = [tableColumn identifier]; 

    NSString * item = [self.toDoList objectAtIndex:row]; 

    if([colId isEqualToString:@"toDoCol"]){ 
     returnVal = item; 
    } 

    return returnVal; 

} 

@end 

回答

1

,我会检查的第一件事是,你NSTableView的IBOutlet中还是在设定的applicationDidFinishLaunching。

NSLog(@"self.toDoListTableView: %@", self.toDoListTableView) 

应该能看到输出,如:

<NSTableView: 0x178941a60> 

如果出口设置正确。

如果您看到'nil'而不是对象,请仔细检查以确保您的NSTableView在Xcode的XIB编辑模式下连接到了您的插座。这里有一个documentation link帮助连接插座。

+0

好的我已经在applicaitonDidFinishLaunching中添加了插座的日志,并且这一点没有,这就解释了为什么我没有看到任何数据。但为什么它是零? – ssloan

+0

进一步检查self.toDoListTableView始终为零,即使在开始时调用numberOfRowsInTableView方法时也是如此。我想它没有正确连接作为插座? – ssloan

+0

我认为这是因为你已经设置了IBOutlet来分配,而不是弱或unsafe_unretained。 –

0

我修正了它 - 我将appDelegate设置为数据源和tableView的委托,但ctrl拖动从tableView到appDelegate,但我没有按住ctrl-拖动另一种方式来实际连接起来我用表格视图声明了出口。现在正在工作。谢谢你的帮助,虽然杰夫。