1

我正在设置一个基于视图的 NSTableView,但是没有调用方法 numberOfRowsInTableView。当我检查它是否被调用时,日志返回 nil。

我已经将我的 AppDelegate 设置为符合数据源和委托协议,并通过 IB 将它们连接到 AppDelegate。当应用程序完成加载时,将初始化我将提供表格的数组。

我的代码如下:

界面

#import <Cocoa/Cocoa.h>

@interface RLTAppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTableView *tableView;
@property (copy) NSArray *nameArray;

@end

执行

#import "RLTAppDelegate.h"

@implementation RLTAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  self.nameArray = [[NSArray alloc] initWithObjects:@"Ryan", nil];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {

  return [self.nameArray count];
  NSLog(@"%lu", self.nameArray.count);
}

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
              row:(NSInteger)row {

NSTextField *result = [tableView makeViewWithIdentifier:@"name" owner:self];
result.stringValue = [self.nameArray objectAtIndex:row];
return result;
}

@end
4

1 回答 1

2

我刚刚做了一个测试项目,看看发生了什么。

如果你在applicationDidFinishLaunching和 上放置一个断点,numberOfRowsInTableView你会看到在收到通知numberOfRowsInTableView之前调用了该方法。applicationDidFinishLaunching

因此,您在填充数组之前要求它。

Normally, you'd have a separate class where this happens and you set up the array when that class in initialised, so this doesn't happen.

于 2012-10-26T16:00:59.823 回答