- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道在 SO 上还有其他关于更改交替行颜色的问题。这很容易,这不是我想做的。
我想在基于 View 的 NSTableView 中绘制自定义交替颜色的行,看起来像 iTunes 11 中的行(行的顶部和底部有小边框,如屏幕截图所示):
我知道我可以子类化 NSTableRowView 并在那里进行自定义绘图。但是,这不是一个可接受的答案,因为自定义行将仅用于表中具有数据的行。换句话说,如果表格只有 5 行,那 5 行将使用我的自定义 NSTableRowView 类,但表格其余部分(为空)中的其余“行”将使用标准交替颜色。在这种情况下,前 5 行将显示挡板,而其余的则不会。不好。
那么,我如何破解 NSTableView 来为填充行和空行绘制这些样式交替的行?
最佳答案
正如您所说,“小边框”实际上可以通过我们的一些作弊轻松完成。因为,如果你仔细观察,每个单元格的顶部是比深色交替行略浅的蓝色,每个单元格的底部是深灰色,你可以子类化 NSTableView,然后重写 - (void) drawRow:(NSInteger)row clipRect:(NSRect)clipRect
:
- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
//Use the drawing code from http://stackoverflow.com/a/5101923/945847, but change the colors to
//look like iTunes's alternating rows.
NSRect cellBounds = [self rectOfRow:row];
NSColor *color = (row % 2) ? [NSColor colorWithCalibratedWhite:0.975 alpha:1.000] : [NSColor colorWithCalibratedRed:0.932 green:0.946 blue:0.960 alpha:1.000];
[color setFill];
NSRectFill(cellBounds);
/* Slightly dark gray color */
[[NSColor colorWithCalibratedWhite:0.912 alpha:1.000] set];
/* Get the current graphics context */
CGContextRef currentContext = [[NSGraphicsContext currentContext]graphicsPort];
/*Draw a one pixel line of the slightly lighter blue color */
CGContextSetLineWidth(currentContext,1.0f);
/* Start the line at the top of our cell*/
CGContextMoveToPoint(currentContext,0.0f, NSMaxY(cellBounds));
/* End the line at the edge of our tableview, for multi-columns, this will actually be overkill*/
CGContextAddLineToPoint(currentContext,NSMaxX(cellBounds), NSMaxY(cellBounds));
/* Use the context's current color to draw the line */
CGContextStrokePath(currentContext);
/* Slightly lighter blue color */
[[NSColor colorWithCalibratedRed:0.961 green:0.970 blue:0.985 alpha:1.000] set];
CGContextSetLineWidth(currentContext,1.0f);
CGContextMoveToPoint(currentContext,0.0f,1.0f);
CGContextAddLineToPoint(currentContext,NSMaxX(self.bounds), 1.0f);
CGContextStrokePath(currentContext);
[super drawRow:row clipRect:clipRect];
}
在一个快速的小表格 View 中完成后,看起来像这样:
但是tableview的顶部和底部怎么办呢?毕竟,它们仍然是丑陋的白色或默认的交替行颜色。好吧,正如 Apple 透露的那样(在题为“有趣的 View Based NSTableView, Basic To Advanced”的演讲中),您可以覆盖 -(void)drawBackgroundInClipRect:(NSRect)clipRect
并做一些数学运算来绘制 tableview 的背景像额外的行。快速实现看起来像这样:
-(void)drawBackgroundInClipRect:(NSRect)clipRect
{
// The super class implementation obviously does something more
// than just drawing the striped background, because
// if you leave this out it looks funny
[super drawBackgroundInClipRect:clipRect];
CGFloat yStart = 0;
NSInteger rowIndex = -1;
if (clipRect.origin.y < 0) {
while (yStart > NSMinY(clipRect)) {
CGFloat yRowTop = yStart - self.rowHeight;
NSRect rowFrame = NSMakeRect(0, yRowTop, clipRect.size.width, self.rowHeight);
NSUInteger colorIndex = rowIndex % self.colors.count;
NSColor *color = [self.colors objectAtIndex:colorIndex];
[color set];
NSRectFill(rowFrame);
/* Slightly dark gray color */
[[NSColor colorWithCalibratedWhite:0.912 alpha:1.000] set];
/* Get the current graphics context */
CGContextRef currentContext = [[NSGraphicsContext currentContext]graphicsPort];
/*Draw a one pixel line of the slightly lighter blue color */
CGContextSetLineWidth(currentContext,1.0f);
/* Start the line at the top of our cell*/
CGContextMoveToPoint(currentContext,0.0f, yRowTop + self.rowHeight - 1);
/* End the line at the edge of our tableview, for multi-columns, this will actually be overkill*/
CGContextAddLineToPoint(currentContext,NSMaxX(clipRect), yRowTop + self.rowHeight - 1);
/* Use the context's current color to draw the line */
CGContextStrokePath(currentContext);
/* Slightly lighter blue color */
[[NSColor colorWithCalibratedRed:0.961 green:0.970 blue:0.985 alpha:1.000] set];
CGContextSetLineWidth(currentContext,1.0f);
CGContextMoveToPoint(currentContext,0.0f,yRowTop);
CGContextAddLineToPoint(currentContext,NSMaxX(clipRect), yRowTop);
CGContextStrokePath(currentContext);
yStart -= self.rowHeight;
rowIndex--;
}
}
}
但是,这会在 tableview 的底部留下同样难看的空白白色!因此,我们还必须覆盖 -(void)drawGridInClipRect:(NSRect)clipRect
。另一个快速实现如下所示:
-(void)drawGridInClipRect:(NSRect)clipRect {
[super drawGridInClipRect:clipRect];
NSUInteger numberOfRows = self.numberOfRows;
CGFloat yStart = 0;
if (numberOfRows > 0) {
yStart = NSMaxY([self rectOfRow:numberOfRows - 1]);
}
NSInteger rowIndex = numberOfRows + 1;
while (yStart < NSMaxY(clipRect)) {
CGFloat yRowTop = yStart - self.rowHeight;
NSRect rowFrame = NSMakeRect(0, yRowTop, clipRect.size.width, self.rowHeight);
NSUInteger colorIndex = rowIndex % self.colors.count;
NSColor *color = [self.colors objectAtIndex:colorIndex];
[color set];
NSRectFill(rowFrame);
/* Slightly dark gray color */
[[NSColor colorWithCalibratedWhite:0.912 alpha:1.000] set];
/* Get the current graphics context */
CGContextRef currentContext = [[NSGraphicsContext currentContext]graphicsPort];
/*Draw a one pixel line of the slightly lighter blue color */
CGContextSetLineWidth(currentContext,1.0f);
/* Start the line at the top of our cell*/
CGContextMoveToPoint(currentContext,0.0f, yRowTop - self.rowHeight);
/* End the line at the edge of our tableview, for multi-columns, this will actually be overkill*/
CGContextAddLineToPoint(currentContext,NSMaxX(clipRect), yRowTop - self.rowHeight);
/* Use the context's current color to draw the line */
CGContextStrokePath(currentContext);
/* Slightly lighter blue color */
[[NSColor colorWithCalibratedRed:0.961 green:0.970 blue:0.985 alpha:1.000] set];
CGContextSetLineWidth(currentContext,1.0f);
CGContextMoveToPoint(currentContext,0.0f,yRowTop);
CGContextAddLineToPoint(currentContext,NSMaxX(self.bounds), yRowTop);
CGContextStrokePath(currentContext);
yStart += self.rowHeight;
rowIndex++;
}
}
当一切都说完了之后,我们在 clipview 的顶部和底部得到了漂亮的小假 TableView 单元格行,看起来有点像这样:
可以找到完整的子类 here .
关于objective-c - 像 iTunes 11 一样绘制 NSTableView 交替行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14178370/
我想交织相同模式和相等长度的两个向量。说: a <- rpois(lambda=3,n=5e5) b <- rpois(lambda=4,n=5e5) 我想交织或交织这两个向量,以创建一个等效于c(a
我有两个矩阵,我想以交替方式交织/交错/堆叠在彼此之上/rbind。 ranks=1:3 names=c("Karl", "Klaus", "Mary") x <- cbind(ranks, name
我在 JavaScript 中有一个字符串,如下所示: "This {{@is}} a $|test$| string with $|@string$| delimiters {{as}} follo
那么,我正在尝试“合并”一个字符串 (a) 和一个字符串列表 (b): a = '1234' b = ['+', '-', ''] 获得所需的输出(c): c = '1+2-34' 所需输出字符串中的
我有几个可变长度的数组,其中填充了表示好数据 block 和坏数据 block 的元组。 input = [ [(True, 0, 400), (False, 400, 500), (True
我有一个包含 3 个单元格的表,我想知道是否可以在每个单元格之间“旋转”数据? 基本上,在设定的时间后,我希望第一个单元格中的数据移动到第二个单元格,第二个单元格数据移动到第三个单元格,第三个单元格数
使用 RichFaces dataTable 组件交替行颜色的最简单方法是什么? 最佳答案 查找组件的 rowClasses 属性。 抱歉.. 阅读 richfaces 文档两秒钟后就明白了。 :)
我想创建一个有 4 列的表格,但只显示 3 列。所以我理想情况下有一个跨越 3 列的 div 并应用溢出:隐藏。单击按钮时,我希望第 3 列(公司 2)向左滑动,并替换为第 4 列(公司 3),以便将
有没有办法在 AngularJS 中替代 ng-repeats ? 例如,假设我有两个数组。 数组1 [a1, a2, a3, ...] 数组2 [b1, b2, b3, ...] 我想针对中型和大屏
我有三个包含元素的列表: a = [[0,1],[2,3],...] b = [[5,6],[7,8],...] c = [] 我想将 a 和 b 中的元素 append 到 c 中以获得: c =
我喜欢在 MATLAB 中绘制经过傅立叶变换的信号。通过 set(gca,'xtick',peaks,'FontSize',12);我可以在 x 轴上显示峰值。但有时,峰值靠得太近,显示峰值的文本与其
我正在使用 CSS Grid 为我的网站构建服务列表。整个网站网格中的一行被分成两个 CSS 网格列。 在第一行的第一列中,有一项服务的描述。在第二列中,有一个代表服务的图像。 每一行,描述和图像交替
我有以下 html 代码: 1 2 3 4 5 6 7 8 我想做的是使用奇数/偶数 nth-child 选择器对它们进行一些交替
在下面的代码示例中,我将 window.status 从“a”替换为“b” function alternateViaIntrvl() { setInterval('alterStatus()
下面的CSS和HTML代码生成 News Interviews ---------------------- Djing Break dance ---------------------- 为什么横
我曾经在 tableView willDisplay cell 方法中使用这段代码,但它没有准确地交替颜色 - 它几乎做到了,但有时仍然搞砸了 1 或 2 个相同的颜色和我不确定。 我发现一些建议在我
我的问题的本质是解决方案太多,我想在围绕它构建基础设施之前找出哪一个在优缺点中胜出。 (为了本论坛的目的进行了简化)这是一个拍卖网站,其中五个拍卖按排名#1-5 存储,#1 是当前特色拍卖。其他四个人
如果可能的话,我正在尝试找出是否有一种方法可以替换内容行分隔符的颜色。 例如: 问题是它必须是自动的,所以我假设可能需要 javascript,但我找不到这样的东西。我知道有些事情表明如果你有类似 t
这个看似简单的问题困扰了我整整 10 年。 (好的,不是每天!) 它在 IE 中运行良好,但在 FF 和 Chrome 中运行不正常,这通常表示代码有问题...我想在两侧都有 DIV 框(它们实际上是
我想找到交替 [0, 1, 0, 1] 所需的最少翻转次数,例如给定 [1, 1, 0, 1]。所以在这种情况下,它是一次翻转。 def solution(A): count = 0
我是一名优秀的程序员,十分优秀!