NSTableView 透明TableView

发布于 2024-10-12 12:31:02 字数 2091 浏览 3 评论 0原文

用户界面要求是, 1 -- 需要显示一个两栏表格,第一栏应显示图像,第二栏应显示一些文本, 2 -- 它应该是透明的,所以它应该显示 NSView 背景,

我的代码编写如下,

我子类 NSTableView 重写以下方法

-(void)awakeFromNib
{

    [[self enclosingScrollView] setDrawsBackground: NO];
    [[self enclosingScrollView] setBorderType:NSNoBorder];

}

- (BOOL)isOpaque {

    return NO;
} 
- (void)drawRect:(NSRect)drawRect
{
    [super drawRect: drawRect];
}

在我的视图中创建表实例如下

@interface MyView : CommUICustomView {
    CustomTableView *myTableView;
}

// 实现

- (void)InitContactTable 
{

    NSRect          scrollFrame = [self bounds];
    NSScrollView*   scrollView  = [[[NSScrollView alloc] initWithFrame:scrollFrame] autorelease];

    [scrollView setBorderType:NSNoBorder];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setHasHorizontalScroller:NO];
    [scrollView setAutohidesScrollers:YES];
    [scrollView setDrawsBackground: NO];

    NSRect          clipViewBounds  = [[scrollView contentView] bounds];
    pOnLineCTView       = [[[CommUITableView alloc] initWithFrame:clipViewBounds] autorelease];

    //[pOnLineCTView setBackGroundImageByName:@"largegears.png"];


    NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    [[firstColumn dataCell] setDrawsBackground:NO];

    [myTableView  addTableColumn:firstColumn];

    NSTableColumn*  secondColumn        = [[[NSTableColumn alloc] initWithIdentifier:@"secondColumn"] autorelease];
    [[secondColumn dataCell] setDrawsBackground:NO];

    [myTableView  addTableColumn:secondColumn];

    [myTableView setDataSource:self];
    [scrollView setDocumentView:pOnLineCTView];

    [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];

    [self addSubview:scrollView];

    [self setAutoresizesSubviews:YES];


}

上面代码的输出是,带有白色背景的表,即使我将 NoofRow 设置为 0,即绘制白色背景, 接下来我尝试,通过重写CustomTableView的drawRect方法在TableView中实现背景,它帮助了我,输出是背景图像,实现透明并显示父视图背景,但是用白色绘制,看起来,它被绘制为白色,因为列或单元格的, 我试图告诉单元格不要绘制背景,但它不起作用, 任何其他方法任何人都可以建议我。

UI Requirement is,
1 -- Need to show a two column table, first col should display an image and 2nd col will have some text with it,
2 -- It should be transparent so it should display the NSView background,

My code is written as below,

I subclass NSTableView overriding following methods

-(void)awakeFromNib
{

    [[self enclosingScrollView] setDrawsBackground: NO];
    [[self enclosingScrollView] setBorderType:NSNoBorder];

}

- (BOOL)isOpaque {

    return NO;
} 
- (void)drawRect:(NSRect)drawRect
{
    [super drawRect: drawRect];
}

In my view creating table instance as below

@interface MyView : CommUICustomView {
    CustomTableView *myTableView;
}

// IMplementation

- (void)InitContactTable 
{

    NSRect          scrollFrame = [self bounds];
    NSScrollView*   scrollView  = [[[NSScrollView alloc] initWithFrame:scrollFrame] autorelease];

    [scrollView setBorderType:NSNoBorder];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setHasHorizontalScroller:NO];
    [scrollView setAutohidesScrollers:YES];
    [scrollView setDrawsBackground: NO];

    NSRect          clipViewBounds  = [[scrollView contentView] bounds];
    pOnLineCTView       = [[[CommUITableView alloc] initWithFrame:clipViewBounds] autorelease];

    //[pOnLineCTView setBackGroundImageByName:@"largegears.png"];


    NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    [[firstColumn dataCell] setDrawsBackground:NO];

    [myTableView  addTableColumn:firstColumn];

    NSTableColumn*  secondColumn        = [[[NSTableColumn alloc] initWithIdentifier:@"secondColumn"] autorelease];
    [[secondColumn dataCell] setDrawsBackground:NO];

    [myTableView  addTableColumn:secondColumn];

    [myTableView setDataSource:self];
    [scrollView setDocumentView:pOnLineCTView];

    [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];

    [self addSubview:scrollView];

    [self setAutoresizesSubviews:YES];


}

The output of above code is , Table with White background, even if i set NoofRow are 0, i.e. White background is getting drawn,
Next i tried, implementing Background in TableView by overriding drawRect method of CustomTableView, it helped me, and output is Background image, to achieve transparent and show Parent view background, but painted with the white color, it seems, its getting white color drawn because of Column or cell,
i tried to tell Cell of Column not to draw Backgorund but it didn't work,
Any other method any guys can suggest me.

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

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

发布评论

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

评论(1

独守阴晴ぅ圆缺 2024-10-19 12:31:02

它的工作原理是,我也重写了drawBackgroundInClipRect,但是在评论了这个方法之后,我可以看到预期的输出,

现在我可以看到透明背景,上面显示表格单元格数据。

Its working, i was overriding drawBackgroundInClipRect also, but after commented this method, i could see the expected output,

Now i can see, transparent background with table cell data being display on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文