gpt4 book ai didi

cocoa - 双击编辑 NSTableView 列标题

转载 作者:行者123 更新时间:2023-12-03 16:05:14 26 4
gpt4 key购买 nike

是否可以通过双击列标题来更改 NSTableView 列的名称?有关最佳方法的任何建议。

我正在尝试:

  1. 设置表格 View 的双击操作以在双击时调用自定义方法
  2. 尝试通过调用 editWithFrame:inView:editor:delegate:event: 来编辑 NSTableHeaderCell 实例。

我不完全确定为什么这会扭曲文本,但是当您双击标题时,它会使文本看起来像这样,没有出现字段编辑器,

editWithFrame:inView:editor:delegate:event: on an NSTableHeaderCell

在 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];
}

}

最佳答案

看来是可行的
您在屏幕截图中看到的是窗口的字段编辑器覆盖单元格的文本字段
编辑器有一个透明的背景,所以这就是它困惑的原因

所以这是交易:

您需要拥有自己的 NSTableHeaderCell 子类来充当字段编辑器的委托(delegate):

@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

在应用程序委托(delegate)的 awakeFromNib 中,不要忘记将 NSTableHeaderCell 设置为可编辑!

- (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:)];
}

剩下的,你就快到了
调用 selectWithFrame 之后,我们将编辑器自定义为具有漂亮的白色不透明背景,
这样我们就看不到它下面的 TextView
至于聚焦环:这是细胞的工作,
我们只是将单元格设置为突出显示状态,这样它就知道现在必须绘制圆环

- (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];
}
}

有关字段编辑器的更多信息:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/WinPanel/Tasks/UsingWindowFieldEditor.html

现在唯一缺少的事情是,如果您在编辑时调整单元格大小,字段编辑器的框架将不会更新......

关于cocoa - 双击编辑 NSTableView 列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14627631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com