我对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
好的我已经在applicaitonDidFinishLaunching中添加了插座的日志,并且这一点没有,这就解释了为什么我没有看到任何数据。但为什么它是零? – ssloan
进一步检查self.toDoListTableView始终为零,即使在开始时调用numberOfRowsInTableView方法时也是如此。我想它没有正确连接作为插座? – ssloan
我认为这是因为你已经设置了IBOutlet来分配,而不是弱或unsafe_unretained。 –