【发布时间】:2018-07-20 09:38:02
【问题描述】:
我有一个 NSTableView,我希望在其中显示信息列表。
目前viewForTableColumn 方法委托从不运行,但numberOfRowsInTableView 会。
我在 ViewController 头中设置了 NSTableViewDelegate 和 NSTableViewDataSource。我将 tableview 委托和数据源设置为 self.有人知道为什么它不会运行吗?我在下面添加了屏幕截图和代码。
ViewController.h
@interface ViewController : NSViewController <NSTableViewDelegate, NSTableViewDataSource>
@property (strong) IBOutlet NSTableView *tableView;
@property (strong, nonatomic) NSMutableArray<App *> *installedApps;
@end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_installedApps = [[NSMutableArray alloc] init];
_tableView.dataSource = self;
_tableView.delegate = self;
// Other stuff that populates the array
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return _installedApps.count;
}
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTableCellView *result = [tableView makeViewWithIdentifier:@"appCell" owner:self];
result.textField.stringValue = @"Hello world";
return result;
}
视图位于容器视图中,我将“appCell”标识符设置为表格单元格视图。
【问题讨论】:
-
顺便说一句:您应该使用访问器。使用
self.installedApps而不是_installedApps,因为后者可能会产生KVO 和子类化问题。 -
填充数组时你会打电话给
[self.tableView reloadData]吗? -
是
_tableViewnil? -
@Willeke Nope not nil.
-
@clemens 谢谢,我将从现在开始这样做。
标签: objective-c macos nstableview