繁体   English   中英

更改NSCell的背景而不更改NSTableColumn中的所有单元格

[英]Change background of NSCell without changing all cells in the NSTableColumn

我正在尝试更改基于单元格的NSTableView特定单元格的背景。 但是,当我尝试仅更改一个单元格的背景色时,它将影响整个列。 是否有一种方法可以分隔单元格和列之间必须存在的任何绑定?

这是我正在使用的代码(带有解释我认为正在发生的事情的注释):

// This allows me to change the background of the cell.

[[[[_tableController1 registerTableView] tableColumnWithIdentifier:@"offset"] dataCellForRow:table1idx] setDrawsBackground:YES];

// This gets the cell within the given table column and row.

[[[[_tableController1 registerTableView] tableColumnWithIdentifier:@"offset"] dataCellForRow:table1idx] setBackgroundColor:[NSColor redColor]];        

// This reloads the table so my changes can be visible.

[[_tableController1 registerTableView] reloadData];

基于单元格的表每列使用一个单元格。 就像各种各样的橡皮图章。 它沿着可见行向下移动,为该行设置单元格,告诉它在该行+列的帧rect中绘制,然后转到下一行。

您应该为表设置一个委托,并使其实现-tableView:willDisplayCell:forTableColumn:row: 在该方法中,为该行设置适当的单元格属性。 您不能只为您认为“特殊”的行设置属性。 如果更改某些行的属性,则还需要将其更改为所有其他行都认为“正常”的属性。

因此,您的方法可能类似于:

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    if ([[atTableColumn identifier] isEqualToString:@"offset"])
    {
        if (rowShouldHaveRedBackground)
        {
            [aCell setDrawsBackground:YES];
            [aCell setBackgroundColor:[NSColor redColor]];
        }
        else
            [aCell setDrawsBackground:NO];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM