Cocoa 是否仅在NSTableView中为填充的行绘制网格线?
如何在NSTableView中仅为已填充的行绘制网格线?当我将网格样式掩码设置为NSTableViewSolidHorizontalGridStyleMask时,它会为所有行绘制网格线,无论是否填充Cocoa 是否仅在NSTableView中为填充的行绘制网格线?,cocoa,Cocoa,如何在NSTableView中仅为已填充的行绘制网格线?当我将网格样式掩码设置为NSTableViewSolidHorizontalGridStyleMask时,它会为所有行绘制网格线,无论是否填充 我正在寻找一些示例代码来实现这一点。我认为您必须对NSTableView进行子类化并重写。另一种可能是重写,但这有点麻烦,因为您必须手动选择行 这将使您朝着正确的方向前进: - (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect {
我正在寻找一些示例代码来实现这一点。我认为您必须对
NSTableView
进行子类化并重写。另一种可能是重写,但这有点麻烦,因为您必须手动选择行
这将使您朝着正确的方向前进:
- (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];
}
}
-(void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect{
//首先执行默认图形
[超级绘图行:行索引clipRect:clipRect];
//检查此行的任何单元格中是否有内容
BOOL-doesHaveContent=NO;
int numCols=[self numberOfColumns];
int-colIndex;
对于(colIndex=0;colIndex
另一种可能是定制
NSCell
;如果每个单元格有内容,它可以在自己周围画一个特殊的边框。但是,如果行中的某些单元格为空,则这不会在整行中划出一条线 到目前为止,我发现最适合我的是以下代码。只需愚弄原始网格绘制代码,使其仅在填充的行上绘制即可
如果需要,子类NSTableView,并重写drawGridInClipRect:(NSRect)clipRect,如下所示:
- (void)drawGridInClipRect:(NSRect)clipRect
{
NSRect lastRowRect = [self rectOfRow:[self numberOfRows]-1];
NSRect myClipRect = NSMakeRect(0, 0, lastRowRect.size.width, NSMaxY(lastRowRect));
NSRect finalClipRect = NSIntersectionRect(clipRect, myClipRect);
[super drawGridInClipRect:finalClipRect];
}
在Swift 3.1中,这是您所需要的全部:
import Cocoa
class GridClipTableView: NSTableView {
override func drawGrid(inClipRect clipRect: NSRect) { }
}
出于某种原因,这里列出的两种解决方案对我都不起作用。我所做的是:
NSTableView
和重写rectOfColumn:
(见下文)- (NSRect)rectOfColumn:(NSInteger)column
{
NSRect value = [super rectOfColumn:column];
NSInteger numberOfRows = [self numberOfRows];
if (numberOfRows == 0) {
value.size.height = 0;
} else {
NSRect firstRect = [self rectOfRow:0];
NSRect lastRect = [self rectOfRow:numberOfRows - 1];
value.size.height = NSMaxY(lastRect) - NSMinY(firstRect);
}
return value;
}
还有一种可能是根据数据源中的实际行数调整表视图的大小。@bavariable:这是一个很好的观点,但在我看来,OP希望出现空行,只是不需要行。这对我根本不起作用。DrawRow方法根本不被调用:(与上面计算myClipRect不同,您是否可以执行以下操作:NSRect finalClipRect=NSIntersectionRect(clipRect,self.bounds)?@sam您可以尝试一下,但我相信它不会起作用。它没有考虑最后一行的位置。Self是表视图,它可能比表中的所有行都高。事实上,根据定义,clipRect应该已经小于或等于Self.bounds。这将创建一个黑色视图来代替空白。我注意到了d当我进行5.0.1的xcode更新时出现此问题。有更新的答案吗?为了在最后一列之后裁剪网格,可以使用以下命令:
NSRect lastColumnRect=[self-rectOfColumn:self.numberOfColumns-1];NSRect finalClipRect=nsMakerRect(0,0,NSMaxX(lastColumnRect),NSMaxY(lastRowRect))
我正在寻找另一种解决方案。我想禁用部分网格线。我不想为部分绘制网格线。我只希望它们位于单元格之间。有什么想法吗?与Obj-C相同:使用空方法覆盖drawGridInClipRect:
,只绘制项目网格。这非常有效,而且比其他解决方案更干净ions!这种方法还切断了列分隔符,而另一种方法则让它们一直绘制到表的底部。