是否可以通过双击列标题来更改NSTableView列的名称?有关最佳方法的任何建议.
我在尝试:
editWithFrame:inView:editor:delegate:event:.我不完全确定为什么这会扭曲文本,但是当你双击标题时它会使文本看起来像这样,没有字段编辑器出现,

在AppDelegate中
-(void)awakeFromNib
{
...
[_tableView setDoubleAction:@selector(doubleClickInTableView:)];
...
}
-(void) doubleClickInTableView:(id)sender
{
NSInteger row = [_tableView clickedRow];
NSInteger column = [_tableView clickedColumn];
if(row == -1){
/* Want to edit the column header on double-click */
NSTableColumn *tableColumn = [[_tableView tableColumns] objectAtIndex:column];
NSTableHeaderView *headerView = [_tableView headerView];
NSTableHeaderCell *headerCell = [tableColumn headerCell];
NSRect cellFrame = [headerView headerRectOfColumn:column];
NSText * fieldEditor = [[headerView window] fieldEditor:YES forObject:nil];
[headerCell editWithFrame:cellFrame inView:headerView editor:fieldEditor delegate:headerCell event:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
似乎可行
你在截图中看到了什么是窗口的字段编辑器覆盖您单元的文本字段
编辑器有一个透明背景,这样就是为什么它搞砸
所以这是交易:
您将需要拥有自己的NSTableHeaderCell子类作为字段编辑器的委托:
@interface NBETableHeaderCell () <NSTextViewDelegate>
@end
@implementation NBETableHeaderCell
- (void)textDidEndEditing:(NSNotification *)notification
{
NSTextView *editor = notification.object;
// Update the title, kill the focus ring, end editing
[self setTitle:editor.string];
[self setHighlighted:NO];
[self endEditing:editor];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if([self isHighlighted])
{
[self drawFocusRingMaskWithFrame:cellFrame inView:controlView.superview];
}
[super drawWithFrame:cellFrame inView:controlView];
}
- (void)drawFocusRingMaskWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[controlView lockFocus];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:cellFrame] fill];
[controlView unlockFocus];
}
@end
Run Code Online (Sandbox Code Playgroud)
在app delegate的awakeFromNib中不要忘记将NSTableHeaderCell设置为Editable!
- (void)awakeFromNib
{
NSTableColumn *newCol = [[NSTableColumn alloc] initWithIdentifier:@"whatever"];
NBETableHeaderCell *hc = [[NBETableHeaderCell alloc] initTextCell:@"Default header text"];
[hc setEditable:YES];
[hc setUsesSingleLineMode:YES];
[hc setScrollable:NO];
[hc setLineBreakMode:NSLineBreakByTruncatingTail];
[newCol setHeaderCell:hc];
[self.tableView addTableColumn:newCol];
[self.tableView setDoubleAction:@selector(doubleClickInTableView:)];
}
Run Code Online (Sandbox Code Playgroud)
其余的,你
在调用selectWithFrame 后几乎就在那里,我们自定义编辑器有一个漂亮的白色不透明背景,
所以我们没有看到它下面的文本视图
和焦点环:它是单元格的工作,
我们只是设置处于突出显示状态的单元格,因此它知道现在必须绘制环
- (void)doubleClickInTableView:(id)sender
{
NSInteger row = [_tableView clickedRow];
NSInteger column = [_tableView clickedColumn];
if(row == -1&& column >= 0)
{
NSTableColumn *tableColumn = [[_tableView tableColumns] objectAtIndex:column];
NSTableHeaderView *headerView = [_tableView headerView];
NBETableHeaderCell *headerCell = [tableColumn headerCell];
// cellEditor is basically a unique NSTextView shared by the window
// that adjusts its style to the field calling him
// it stands above the text field's view giving the illusion that you are editing it
// and if it has no background you will see the editor's NSTextView overlaying the TextField
// wich is why you have that nasty bold text effect in your screenshot
id cellEditor = [self.window fieldEditor:YES forObject:self.tableView];
[headerCell setHighlighted:YES];
[headerCell selectWithFrame:[headerView headerRectOfColumn:column]
inView:headerView
editor:cellEditor
delegate:headerCell
start:0
length:headerCell.stringValue.length];
[cellEditor setBackgroundColor:[NSColor whiteColor]];
[cellEditor setDrawsBackground:YES];
}
}
Run Code Online (Sandbox Code Playgroud)
有关字段编辑器的更多信息,请访问:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/WinPanel/Tasks/UsingWindowFieldEditor.html
现在唯一缺少的就是如果在编辑时调整单元格的大小,字段编辑器的框架将不会更新...
| 归档时间: |
|
| 查看次数: |
2470 次 |
| 最近记录: |