添加透明背景的行

发布于 2024-10-19 03:36:15 字数 438 浏览 1 评论 0原文

我有一个 NSTableView,下面有一个“添加”按钮。当我单击该按钮时,一个新行将添加到表中并准备好供用户输入。

该行显示为白色。我可以将行的颜色设置为透明颜色吗?这可能吗?我不知道该怎么做。

我将表格设置为透明的代码:

[myTable setBackgroundColor:[NSColor clearColor]];
[[myTable enclosingScrollView] setDrawsBackground: NO];

添加行的代码:

[myTableArray addObject:@""];
[myTable reloadData];
[myTable editColumn:0 row:[myTableArray count]-1 withEvent:nil select:YES];

I have an NSTableView, with an "add" button below it. When I click on the button, a new row gets added to the table and is ready for user input.

The row appears in a white color. Can I set the color of the row to a transparent color? Is this possible? I cannot figure out how to do this.

My code for setting my table to be transparent:

[myTable setBackgroundColor:[NSColor clearColor]];
[[myTable enclosingScrollView] setDrawsBackground: NO];

Code for adding a row:

[myTableArray addObject:@""];
[myTable reloadData];
[myTable editColumn:0 row:[myTableArray count]-1 withEvent:nil select:YES];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有你我更好 2024-10-26 03:36:15

尝试将单元格的背景颜色设置为透明

[cell setBackgroundColor:[UIColor clearColor]];

这对我有用

try setting the cell's background color transparent

[cell setBackgroundColor:[UIColor clearColor]];

it works for me

梦途 2024-10-26 03:36:15

我认为您可能需要进行一些子类化才能完成您想要做的事情。

通过子类化您的 NSTableView,您可以重写 preparedCellAtColumn:row: 方法,如下所示:

- (NSCell*) preparedCellAtColumn:(NSInteger)column row:(NSInteger)row {
    NSTextFieldCell *edit_field;

    edit_field = (NSTextFieldCell*) [super preparedCellAtColumn:column row:row];
    if ( [self editedRow] == row && [self editedColumn] == column  ) {
        [edit_field setBackgroundColor:[NSColor clearColor]];
        [edit_field setDrawsBackground:NO];

    }

    return edit_field;
}

但是,NSTableView 文档表明您的单元格调用了另一个方法,该方法似乎会重置颜色。 (editWithFrame:inView:editor:delegate:event:) 创建覆盖此方法的 NSTextViewCell 子类可能会满足您的要求。

编辑
搜索文档我发现了这个:

如果接收者不是文本类型的 NSCell 对象,则不执行任何编辑。否则,字段编辑器 (textObj) 的大小设置为 aRect,其超级视图设置为 controlView,因此它完全覆盖接收器。

因此,在这种情况下,您需要自定义的是字段编辑器,它会覆盖您在 NSTableView 或单元格上执行的任何显示更改。

字段编辑器由窗口委托的方法 windowWillReturnFieldEditor:toObject 返回:

这应该允许您在将编辑的单元格返回到 NSTableView

EDIT 之前设置其属性
尝试此方法无济于事,但可能会有所帮助:

-(id) windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)client{
    NSText *editor =  [window fieldEditor:YES forObject:client];
    [editor setBackgroundColor:[NSColor clearColor]];
    [editor setDrawsBackground:NO];
    return [editor autorelease];
}

I think you may have to do some subclassing to accomplish what you're trying to do.

By subclassing your NSTableView you can override the preparedCellAtColumn:row: method like so:

- (NSCell*) preparedCellAtColumn:(NSInteger)column row:(NSInteger)row {
    NSTextFieldCell *edit_field;

    edit_field = (NSTextFieldCell*) [super preparedCellAtColumn:column row:row];
    if ( [self editedRow] == row && [self editedColumn] == column  ) {
        [edit_field setBackgroundColor:[NSColor clearColor]];
        [edit_field setDrawsBackground:NO];

    }

    return edit_field;
}

However, the NSTableView documentation indicates that your cell has another method called, which seems to reset the color. (editWithFrame:inView:editor:delegate:event:) Creating a subclass of NSTextViewCell that overrides this method may do what you're looking for.

EDIT
Searching through the documentation I found this:

If the receiver isn’t a text-type NSCell object, no editing is performed. Otherwise, the field editor (textObj) is sized to aRect and its superview is set to controlView, so it exactly covers the receiver.

So what you need to customize in this case is the field editor, which is covering up any display changes you're performing on the NSTableView or the cell.

The field editor is returned by the window delegate's method windowWillReturnFieldEditor:toObject:

This should let you set the properties of the edited cell before returning it to the NSTableView

EDIT
Tried this to no avail but might help out:

-(id) windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)client{
    NSText *editor =  [window fieldEditor:YES forObject:client];
    [editor setBackgroundColor:[NSColor clearColor]];
    [editor setDrawsBackground:NO];
    return [editor autorelease];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文