我有一个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”标识符设置为Table Cell View。
数组_installedApps
为空,numberOfRowsInTableView:
返回0.因此,没有调用tableView:viewForTableColumn:
,因为没有要显示的行。没有行也意味着没有列。
您还应确保已将表视图配置为基于表视图的属性检查器的视图。
我无法在屏幕截图中看到它,但是...是具有appCell标识符的视图层次结构(表格单元格视图)的突出显示的行?
[分钟通过...]
哎呀;抱歉。我看到你已经注意到了上述情况。
我问的原因是我从你的代码中创建了一个新项目,将数组类型从App更改为NSString,在故事板中添加了一列表视图,将其链接到代码,在数组中添加了一些内容 - viewDidLoad,并且 - 一旦我将appCell标识符放在正确的位置(duh) - 它一切正常。
这是非常奇怪的,但我有同样的问题,一切正确连接,被调用的行数,但不是viewForTableColumn ...最后,以下答案被证明是解决方案:
https://stackoverflow.com/a/13091436/3963806
基本上,我已经直接从对象库中设置了tableview,没有布局约束等等......一旦我添加了布局约束,该方法就开始被调用了...我看到的超级奇怪,点击“行”但是他们没有正确填充...我认为这取决于链接答案中提到的剪辑。