向我的NSTableView添加一个可编辑的NSTextFieldCell [英] Adding an editable NSTextFieldCell to my NSTableView

查看:1581
本文介绍了向我的NSTableView添加一个可编辑的NSTextFieldCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTableView显示一些代表我的自定义对象的信息。我不使用绑定。



通常,我创建我自己的NSCells来显示数据,但是一旦我在一个NSTextFieldCell之后,将显示对象的字符串值,用户编辑它。



我可以使用下面的代码成功添加NSTextFieldCell,但它不可编辑。

  NSTextFieldCell * textField = [[NSTextFieldCell alloc] init]; 
[textField setFont:[NSFont fontWithName:@Helvetica Boldsize:13]];
[textField setTextColor:[NSColor colorWithDeviceRed:0.1058823529 green:0.2117647059 blue:0.3294117647 alpha:1.0]];
[textField setStringValue:projectName];
[textField setEditable:YES];
[textField setBordered:NO];

[textField drawWithFrame:textRect inView:controlView];
[textField release];

有人可以帮我吗?

NSTableView 时,该单元格的单个实例将从之前的位置跳转,并处理您的编辑。



正如你所说,这样做适用于单元格的视觉外观(绘图),并且适用于 NSTextField 因为每个视图每个 NSTextField 必须只有一个单元格,因此,当你想要编辑它。



但是在这种情况下,您正在创建一个单元格,绘制它,然后通过在代码结束时释放它从内存中踢出。那么如何预期这个单元格,你设置为可编辑的,当你尝试编辑它?



在设置表视图时尝试创建一个单元格,并使用此选项将您的自定义单元格设置为
右表格列:

   - (void)setDataCell:(NSCell *)aCell 

或者,你可以子类化 NSTextFieldCell 并在那里做你的定制, (或XCode 4,如果你在流血的边缘!)


I have an NSTableView which displays some information representing a custom object of mine. I am not using bindings.

Typically, I create my own NSCells to display data, but for once I'm after an NSTextFieldCell that will display a string value of the object, as well as let the user edit it.

I can successfully add the NSTextFieldCell using the code below, but it is not editable.

NSTextFieldCell *textField = [[NSTextFieldCell alloc] init];
[textField setFont:[NSFont fontWithName:@"Helvetica Bold" size:13]];
[textField setTextColor:[NSColor colorWithDeviceRed:0.1058823529 green:0.2117647059 blue:0.3294117647 alpha:1.0]];
[textField setStringValue:projectName];
[textField setEditable:YES];
[textField setBordered:NO];

[textField drawWithFrame:textRect inView:controlView];
[textField release];

Could someone please help me with this?

解决方案

NSTextFieldCell is implemented with the flyweight pattern (I read about it in the "Cocoa Design Patterns" book) and each column has only one instance of a cell. You can see some kind of evidence of this when you edit it in interface builder. When you click to edit an NSTableView, that single instance of the cell jumps in from where it was before and handles the editing for you.

As you say, doing this works for the visual appearance (drawing) of the cell, and works for NSTextField as well because each NSTextField must have just one cell per view, and therefore it's around when you want to edit it.

However in this case, you are creating a cell, drawing it, then kicking it out of memory by releasing it at the end of your code. So how do you expect this cell which you have set as editable to be around when you try to edit it? It doesn't exist anymore.

Try creating a single cell when the table view is set up and setting your custom cell to the right table column using this:

- (void)setDataCell:(NSCell *)aCell

Alternatively you could subclass NSTextFieldCell and do your customization there, and set the cell class for the column in interface builder (or XCode 4 if you're on the bleeding edge!)

这篇关于向我的NSTableView添加一个可编辑的NSTextFieldCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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