2012-06-23 75 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-拖動另一種方式來實際連接起來我用表格視圖聲明瞭出口。現在正在工作。謝謝你的幫助,雖然傑夫。