NSTableView如何通过代码设置内容模式(基于视图或基于单元格)? [英] How does the NSTableView set content Mode(view-based or cell-based) by code?

查看:521
本文介绍了NSTableView如何通过代码设置内容模式(基于视图或基于单元格)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为标题。
NSTableView如何通过代码设置内容模式(基于视图或基于单元格)?



感谢您的帮助


< c> NSTableView 默认为基于单元格,这对于向后兼容性是有意义的。表视图是基于视图的,当表视图委托实现 -tableView:viewForTableColumn:row:。您可以通过编程方式轻松测试创建如下表视图:

  @implementation BAVAppDelegate 

- void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView * contentView = self.window.contentView;
NSTableView * tableView = [[NSTableView alloc] initWithFrame:(NSRect){{50,NSMaxY(contentView.frame) - 200},{400,200}}];
tableView.dataSource = self;
tableView.delegate = self;
[contentView addSubview:tableView];

NSTableColumn * column = [[NSTableColumn alloc] initWithIdentifier:@column];
column.width = 400;
[tableView addTableColumn:column];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return 3;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
return [NSString stringWithFormat:@ ld,row];
}

// - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// NSTextField * textField = [[NSTextField alloc] initWithFrame:(NSRect){。size = {100,15}}];
// textField.stringValue = [NSString stringWithFormat:@%ld,row];
// return textField;
//}

@end

代码与注释掉的委托方法,你会得到一个基于单元格的表视图:





如果取消注释委托方法,您将获得基于视图的表视图:



>



-tableView:viewForTableColumn:row:的文档说明


如果您希望对表视图中的单元格使用NSView对象而不是NSCell对象,则需要此方法。单元格和视图不能在同一个表视图中混合。


这暗示了它是决定表视图是否基于细胞或视图。


As title. How does the NSTableView set content Mode(view-based or cell-based) by code?

Thanks for helping

解决方案

NSTableView defaults to being cell-based, which makes sense for backwards compatibility. Table views are view-based when the table view delegate implements -tableView:viewForTableColumn:row:. You can easily test by programmatically creating a table view as follows:

@implementation BAVAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSView *contentView = self.window.contentView;
    NSTableView *tableView = [[NSTableView alloc] initWithFrame:(NSRect){{50, NSMaxY(contentView.frame) - 200}, {400, 200}}];
    tableView.dataSource = self;
    tableView.delegate = self;
    [contentView addSubview:tableView];

    NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"];
    column.width = 400;
    [tableView addTableColumn:column];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return 3;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    return [NSString stringWithFormat:@"%ld", row];
}

//- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
//    NSTextField *textField = [[NSTextField alloc] initWithFrame:(NSRect){.size = {100, 15}}];
//    textField.stringValue = [NSString stringWithFormat:@"%ld", row];
//    return textField;
//}

@end

If you run this code with that delegate method commented out, you get a cell-based table view:

And if you uncomment that delegate method, you get a view-based table view:

The documentation for -tableView:viewForTableColumn:row: states that

This method is required if you wish to use NSView objects instead of NSCell objects for the cells within a table view. Cells and views can not be mixed within the same table view.

which hints at it being the condition that determines whether a table view is cell- or view-based.

这篇关于NSTableView如何通过代码设置内容模式(基于视图或基于单元格)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆