18

ビューベースのnstableviewがあります。以下のコードを使用した条件に基づいて、行全体に色を付けたい

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row 
{
    NSTableRowView *view = [[NSTableRowView alloc] initWithFrame:NSMakeRect(1, 1, 100, 50)];

    [view setBackgroundColor:[NSColor redColor]];
    return view;;
}

NSTableRowViewデリゲート メソッドが呼び出されますが、テーブルはデリゲート メソッドによって返されたものを使用していないようです。

ここでの主な目的は、いくつかの条件に基づいて行全体を色付けすることです。上記の実装で何が問題になっていますか?

4

4 に答える 4

39

これにヒットし、カスタム NSTableRowView backgroundColor が必要な人には、2 つの方法があります。

  1. カスタム描画が必要ない場合rowView.backgroundColor- (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)rowNSTableViewDelegate.

    例:

    - (void)tableView:(NSTableView *)tableView
        didAddRowView:(NSTableRowView *)rowView
               forRow:(NSInteger)row {
    
        rowView.backgroundColor = [NSColor redColor];
    
    }
    
  2. カスタム描画が必要な場合はNSTableRowView、desired で独自のサブクラスを作成してくださいdrawRect。次に、 に次を実装しますNSTableViewDelegate

    例:

    - (NSTableRowView *)tableView:(NSTableView *)tableView
                    rowViewForRow:(NSInteger)row {
        static NSString* const kRowIdentifier = @"RowView";
        MyRowViewSubclass* rowView = [tableView makeViewWithIdentifier:kRowIdentifier owner:self];
        if (!rowView) {
            // Size doesn't matter, the table will set it
            rowView = [[[MyRowViewSubclass alloc] initWithFrame:NSZeroRect] autorelease];
    
            // This seemingly magical line enables your view to be found
            // next time "makeViewWithIdentifier" is called.
            rowView.identifier = kRowIdentifier; 
        }
    
        // Can customize properties here. Note that customizing
        // 'backgroundColor' isn't going to work at this point since the table
        // will reset it later. Use 'didAddRow' to customize if desired.
    
        return rowView;
    }
    
于 2013-10-09T00:05:55.153 に答える
1

最終的に以下のように動作しました

    - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row

{
        NSView *cellView = (NSView*) [tableView makeViewWithIdentifier:[tableColumn identifier] owner:[tableView delegate]];
        CALayer *viewLayer = [CALayer layer];
        [viewLayer setBackgroundColor:[[NSColor redcolor] CGColor]];
        [cellView setWantsLayer:YES]; 
        [cellView setLayer:viewLayer];
        return cellView;
    }

注意してください.. https://gist.github.com/707921またはhttp://forrst.com/posts/CGColor_Additions_for_NSColor-1eWで見つけることができる nscolor を cgcolor に変換する必要があります。

于 2012-06-07T03:32:48.480 に答える
0

WWDC 2011 のビュー ベースのテーブルビューに関するプレゼンテーションを見ると、主なアイデアは Interface Builder でビューを作成し、そこから取得することであることがわかります。何かのようなもの:

[tableView makeViewWithIdentifier:@"GroupRow" owner:self];

ビューを取得したら、そのプロパティを設定して返すだけです。

この例では、独自の識別子があることに注意してください。それを設定することを忘れないでください。ただし、自動識別子を使用することもできます。

WWDC への直接リンクが機能するかどうかはわかりませんが、メイン ページはこちら: https://developer.apple.com/videos/wwdc/2011/で、「View Based NSTableView Basic to Advanced」を検索すると、 "、あなたはそれを見つけるでしょう。一見の価値ありです。

于 2012-06-06T08:47:01.333 に答える