在特定的NSTableview单元格中对角绘制线条

Son*_*onu 1 cocoa nstableviewcell

有没有办法在NSTableview单元格中对角画线.请你发贴样本来做这个.我是Mac开发的新手.请帮我解决这个问题.

提前致谢.......

spu*_*fle 8

是的,很容易.

您需要创建一个子类,NSTextFieldCell其实际上是NSTableView用于显示文本的单元格类型.

对类进行子类化会创建该类的新版本,该版本执行原始类所做的所有操作以及更多操作.

这是使用Xcode 4.如果您使用Xcode 3,请告诉我.

在Xcode中,选择File> New> New File ...创建一个新文件.

新文件

在弹出的工作表中选择Objective-C Class并单击Next.

Objc类

使它成为子类NSTextFieldCell,这是我们将修改的副本.点击下一步.

子类

您可以将其保存为您想要的任何内容,但出于本教程的目的,请将其另存为MyDiagonalLinedTextFieldCell.点击保存.

保存

应该弹出两个新文件.

档

单击.m文件.这是实现文件,它告诉我们类中的方法是做什么的.其内容应类似于以下内容:

//
//  MyDiagonalLinedTextFieldCell.m
//  CustomCell
//
//  Created by spudwaffle on 7/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MyDiagonalLinedTextFieldCell.h"

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

init方法下面添加一个drawInteriorWithFrame: inView:方法.drawInteriorWithFrame: inView:每次单元格需要在屏幕上渲染时,应用程序都会调用该方法.

您的代码现在应该如下所示:

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

}

@end
Run Code Online (Sandbox Code Playgroud)

你需要做的第一件事就是画一个标准NSTextFieldCell.这可以通过调用:

[super drawInteriorWithFrame:cellFrame inView:controlView];
Run Code Online (Sandbox Code Playgroud)

NSTextFieldCell在程序想要的确切区域中绘制了一个法线.

现在,我们需要绘制自定义线.让它们分开5个像素,使它们像素宽1像素.这需要一个for循环!

for (int i = 0; i < cellFrame.size.width/5; i ++) {

}
Run Code Online (Sandbox Code Playgroud)

这使得a int等于0,每次循环运行时都会增加该计数,并在i达到需要绘制的行数时停止.

接下来,输入绘图代码来绘制线条.

for (int i = 0; i < cellFrame.size.width/5; i ++) {
        NSBezierPath *path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(i * 5, cellFrame.origin.y)];
        [path lineToPoint:NSMakePoint((i * 5) + 2, cellFrame.origin.y + cellFrame.size.height)];
        [[NSColor grayColor]set];
        [path setLineWidth:1];
        [path stroke];
}
Run Code Online (Sandbox Code Playgroud)

这个:

  1. 创建一个NSBezierPath,用于绘制线条和形状.
  2. 将路径的起点移动到单元格的下边缘.
  3. 在单元格的顶部边缘绘制一条线.
  4. 将绘图颜色设置为灰色.
  5. 将绘图线宽设置为1.
  6. 绘制线条.

由于for循环,它为每一行反复执行此操作.

这是完成的MyDiagonalLinedTextFieldCell.m文件.你现在不需要担心这个问题.h.

#import "MyDiagonalLinedTextFieldCell.h"

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [super drawInteriorWithFrame:cellFrame inView:controlView];
    for (int i = 0; i < cellFrame.size.width/5; i ++) {
        NSBezierPath *path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(i * 5, cellFrame.origin.y)];
        [path lineToPoint:NSMakePoint((i * 5) + 2, cellFrame.origin.y + cellFrame.size.height)];
        [[NSColor grayColor]set];
        [path setLineWidth:1];
        [path stroke];
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

现在,我们需要在表视图中设置单元格以使用此类.

点击你的MainMenu.xib文件.单击表视图行中的单元格,直到它变为蓝色.

蓝细胞

然后,点击右侧栏中的按钮,如下所示: 按键

班级更改为MyDiagonalLinedTextFieldCell并按Enter键.

班级变化

现在点击运行,享受你的劳动成果!

自定义单元格窗口

弄乱绘图代码,直到获得所需的确切行数.

如果您有任何疑问,请联系我.