簡體   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