xcode 无法通过单击使 NSTableView 可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14665138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't make NSTableView editable via click
提问by Joey FourSheds
I'm trying to make a simple NSTableView (text only) where the cell views can be clicked upon to edit the text. All the tutorials and related questions here suggest that this is the automatic behaviour, but I can't get it.
我正在尝试制作一个简单的 NSTableView(仅限文本),可以在其中单击单元格视图来编辑文本。这里的所有教程和相关问题都表明这是自动行为,但我无法理解。
I have no trouble linking my delegate and data-sources; I can populate all the cells programmatically, and I can figure out what to do with the new text that is entered upon editing. ... I just can't get the text box to open for editing!
我可以毫不费力地链接我的委托和数据源;我可以以编程方式填充所有单元格,并且可以弄清楚如何处理编辑时输入的新文本。...我只是无法打开文本框进行编辑!
(The NSTable columns are marked as editable in IB)
(NSTable 列在 IB 中被标记为可编辑)
Thanks for any clues.
感谢您提供任何线索。
采纳答案by Joey FourSheds
In the tableView:viewForTableColumn delegate method I had to specifically set each cell to be editable.
在 tableView:viewForTableColumn 委托方法中,我必须专门将每个单元格设置为可编辑。
It appears to work without problem after a couple of weeks' use, but I still think there must be a less laborious method somehow.
使用几周后,它似乎可以正常工作,但我仍然认为一定有一种不那么费力的方法。
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTableCellView *cellView;
if( [tableColumn.identifier isEqualToString:@"word"] )
{
wordCellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
cellView = wordCellView;
[cellView.textField setEditable:YES]; // Make Cell Editable!
}
}
回答by jLi
You can do this in Interface Builder by selecting the Text Field Cell, then using the Attributes Inspector, scroll down to the drop down menu entitled "Behavior" and choose "Editable".
您可以在界面生成器中通过选择文本字段单元格,然后使用属性检查器,向下滚动到标题为“行为”的下拉菜单并选择“可编辑”来执行此操作。
This alone will enable you to be able to double-click on individual cells and have them become an editable TextField.
仅此一项即可使您能够双击单个单元格并使它们成为可编辑的 TextField。
To make the edits you make have an effect, you will also have to implement the following method from the NSTableViewDelegate Protocol: - (void)tableView:setObjectValue:forTableColumn:row:
为了使您所做的编辑生效,您还必须从 NSTableViewDelegate 协议中实现以下方法: - (void)tableView:setObjectValue:forTableColumn:row:
[See Apple Docs for this function] (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSTableViewDataSource/tableView:objectValueForTableColumn:row:)
As usual, this method will have to be implemented in the object that you have set as the delegate for the NSTableView object.
像往常一样,此方法必须在您设置为 NSTableView 对象的委托的对象中实现。
[See Apple Docs on delegates in case you want a review of them] (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18)
[如果您想查看代表,请参阅 Apple Docs 上的代表] ( https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/ uid/TP40002974-CH7-SW18)