我對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。 –