本文介绍了在NSTableView中只为填充行绘制网格线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在NSTableView中只为填充的行绘制网格线?当我将网格样式掩码设置为NSTableViewSolidHorizo​​ntalGridStyleMask时,它会为所有行绘制网格线,无论是否填充。



我正在寻找一些示例代码来执行此操作。

解决方案

我认为你必须子类 NSTableView 并覆盖。另一种可能性是覆盖,但这有点更笨拙,因为你必须手动选择行。 / p>

这应该让你走正确的方向:

   - void drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect {
//首先做默认绘图
[super drawRow:rowIndex clipRect:clipRect];

//检查此行中任何单元格中是否有内容
BOOL doesHaveContent = NO;
int numCols = [self numberOfColumns];
int colIndex;
for(colIndex = 0; colIndex< numCols; colIndex ++){
NSCell * cell = [self preparedCellAtColumn:colIndex
row:rowIndex];
if([cell objectValue]!= nil){
doesHaveContent = YES;
break;
}
}

if(doesHaveContent){
NSRect rowRect = [self rectOfRow:rowIndex];
NSBezierPath * gridPath = [NSBezierPath bezierPath];
//忽略clipRect,因为这不是很多绘图
[gridPath moveToPoint:rowRect.origin];
[gridPath lineToPoint:NSMakePoint(rowRect.origin.x + rowRect.size.width,
rowRect.origin.y)];
[gridPath moveToPoint:NSMakePoint(rowRect.origin.x,
rowRect.origin.y + rowRect.size.height)];
[gridPath lineToPoint:NSMakePoint(rowRect.origin.x + rowRect.size.width,
rowRect.origin.y + rowRect.size.height)];
[myGridColor set];
[gridPath stroke];
//你也可以这样做:
// [myGridColor set];
// [[NSBezierPath bezierPathWithRect:rowRect] stroke];
}
}

另一种可能性是自定义 NSCell ;如果它有内容,每个单元格可以围绕自身绘制一个特殊的边框。这不会画一条线穿过整个行,虽然,如果在行中有一些单元格是空的。


How can I draw grid lines in an NSTableView only for rows that are populated? When I set the grid style mask to NSTableViewSolidHorizontalGridStyleMask, it draws grid lines for all rows whether populated or not.

I'm looking for a bit of sample code to do this.

解决方案

I think you will have to subclass NSTableView and override drawRow:clipRect:. Another possibility would be overriding drawGridInClipRect:, but that's a bit more unwieldy, because you'll have to manually pick out the rows.

This should get you going in the right direction:

- (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect {
    // First do the default drawing
    [super drawRow:rowIndex clipRect:clipRect];

    // Check if there's content in any of the cells in this row
    BOOL doesHaveContent = NO;
    int numCols = [self numberOfColumns];
    int colIndex;
    for( colIndex = 0; colIndex < numCols; colIndex++ ){
        NSCell * cell = [self preparedCellAtColumn:colIndex
                                               row:rowIndex];
        if( [cell objectValue] != nil ){
            doesHaveContent = YES;
            break;
        }
    }

    if( doesHaveContent ){
        NSRect rowRect = [self rectOfRow:rowIndex];
        NSBezierPath * gridPath = [NSBezierPath bezierPath];
        // Ignoring clipRect because this isn't a lot of drawing
        [gridPath moveToPoint:rowRect.origin];
        [gridPath lineToPoint:NSMakePoint(rowRect.origin.x + rowRect.size.width,
                                          rowRect.origin.y)];
        [gridPath moveToPoint:NSMakePoint(rowRect.origin.x,
                                          rowRect.origin.y + rowRect.size.height)];
        [gridPath lineToPoint:NSMakePoint(rowRect.origin.x + rowRect.size.width,
                                          rowRect.origin.y + rowRect.size.height)];
        [myGridColor set];
        [gridPath stroke];
        // You could also just do:
        // [myGridColor set];
        // [[NSBezierPath bezierPathWithRect:rowRect] stroke];
    }
}

Yet another possibility would be to have a custom NSCell; each cell could draw a special border around itself if it had content. This wouldn't draw a line across the whole row, though, if there were some cells in the row that were empty.

这篇关于在NSTableView中只为填充行绘制网格线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 14:07