基于视图的NSTableView编辑 [英] View based NSTableView editing

查看:362
本文介绍了基于视图的NSTableView编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我做错了什么。由于我没有找到任何其他问题(甚至文档),这似乎通常工作没有问题为其他人。

I'm not sure what I'm doing wrong. Since I couldn't find any other questions (or even documentation) about this, it seems do be normally working without problems for other people.

我只是试图得到一个基于视图的NSTableView来支持编辑它的内容。也就是说该应用程序显示一个列和几行的NSTableView,其中包含具有某些内容的NSTextField。我想能够(双击)单元格并编辑单元格的内容。因此基本上是基于单元格的NSTableView的正常行为,其中实现 tableView:setObjectValue:forTableColumn:row:方法。

I'm simply trying to get a view based NSTableView to do support editing of it's content. I.e. the app displays a NSTableView with one column and several rows, containing a NSTextField with some content. I want to be able to (double) click on a cell and edit the cell's content. So basically the normal behavior of a cell based NSTableView where the tableView:setObjectValue:forTableColumn:row: method is implemented.

我分析了TableViewPlayground中的TableViewPlayground示例代码中的Complex TableView示例(它支持单元格内容的编辑),但是我找不到启用编辑的设置/ code / switch。

I analyzed the Complex TableView example in the TableViewPlayground sample code from Apple (which is supporting editing of cell content), but I cannot find the setting/code/switch which is enabling the editing.

这是一个简单的示例项目(Xcode 6.1.1,SDK 10.10,基于storyboard):

Here's a simple sample project (Xcode 6.1.1, SDK 10.10, storyboard based):

标题:

#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController

@property (weak) IBOutlet NSTableView *tableView;

@end

实施:

#import "ViewController.h"

@implementation ViewController
{
    NSMutableArray* _content;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _content = [NSMutableArray array];

    for(NSInteger i = 0; i<10; i++) {
        [_content addObject:[NSString stringWithFormat:@"Item %ld", i]];
    }
}

#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return _content.count;
}

#pragma mark - NSTableViewDelegate
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTableCellView* cell = [tableView makeViewWithIdentifier:@"CellView" owner:self];
    cell.textField.stringValue = _content[row];
    return cell;
}

- (IBAction)endEditingText:(id)sender {
    NSInteger row = [_tableView rowForView:sender];
    if (row != -1) {
        _content[row] = [sender stringValue];
    }
}
@end

故事板文件看起来像this:

The storyboard file looks like this:

数据源和表视图的委托被设置为视图控制器。
运行此应用程序时,表视图显示10个测试行,但无法编辑其中一行。

The datasource and delegate of the table view are set to the view controller. When running this app, the table view displays the 10 test rows, but it is not possible to edit one of the rows.

为什么?我在这里错过了什么?

Why is that? What did I miss here?

我选中NSTableView的所有属性(和它的内容)与Apple的TableViewPlayground示例中的属性相同。经过几个小时的文档和互联网搜索有用的提示没有任何成功,我有点沮丧。所有你可以找到基于视图的NSTableViews是不可编辑的样本或可编辑内容的非常模糊的信息。当然,有大量的信息,文档和示例可编辑,基于单元格的NSTableViews ...

I double checked all attributes of the NSTableView (and it's contents) to be the same as in the TableViewPlayground sample from Apple. And after several hours of searching the documentation and internet for helpful hints without any success, I'm kind of frustrated. All you can find on view based NSTableViews are non-editable samples or very vague information on editable content. And of course, there is tons of information, documentation and samples on editable, cell based NSTableViews...

我的示例项目的zip可以下载:
TableTest.zip

A zip with my sample project is downloadable here: TableTest.zip

推荐答案

默认情况下,每个单元格( NSTableCellView 的实例)都有一个 NSTextField 坐在它。当您编辑单元格时,您实际编辑的是此文本字段。 Interface Builder使此文本字段不可编辑:

By default, each cell (instance of NSTableCellView) has an NSTextField sitting on it. When you're editing the cell, what you're in fact editing is this text field. Interface Builder makes this text-field non-editable:

所有你需要做的是将行为弹出窗口设置为 code>。现在,您可以使用返回匹配或单一来编辑文本字段。

All you need to do is set the Behaviour pop-up to Editable. Now you can edit the text-field with a return hit or a single-click.

这篇关于基于视图的NSTableView编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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