在使用 Core Graphics 为网格添加垂直线时设置表格视图中的背景颜色
我试图通过覆盖 UITableView 中默认提供的标准水平线顶部的垂直线来使用 UITableView 创建网格样式表。我正在使我的代码适应此博客提供的示例: http://www.iphonedevx.com/ ?p=153。
首先,绘制垂直线(与水平线颜色相同)的代码在与表视图控制器分开的文件中实现,称为“MyTableCell.m”:
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Use the same color and width as the default cell separator for now
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.25);
for (int i = 0; i < [columns count]; i++) {
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}
CGContextStrokePath(ctx);
[super drawRect:rect];
}
接下来,在表视图控制器 tableView 方法中,我们调用 [cell addColumn :50] 在视图边界左侧向左绘制一条垂直线 50 像素:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:50];
label.tag = LABEL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:@"%d", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(60.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:120];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text = [NSString stringWithFormat:@"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
return cell;
}
我试图将整个视图的背景从白色(默认)更改为黑色。我尝试通过在表视图控制器的 viewDidLoad 方法中将 self.view.backgroundColor 设置为黑色来实现此目的:
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
}
但是,当我这样做时,垂直线消失......
我认为以某种方式在 viewDidLoad 中设置 backgroundColor 会更改 CurrentContext当我们到达drawRect:方法时,但我不知道如何调整。我尝试通过drawRect中的CGContextSetFillColorWithColor()设置背景颜色:像这样:
GContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, tableBackgroundColor());
CGContextFillRect(ctx, self.bounds);
其中tableBackgroundColor()是黑色的,如下所示:
CGColorRef tableBackgroundColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceRGBColor(0.0, 0.0, 0.0, 1.0); // black
}
return c;
}
CGColorRef CreateDeviceRGBColor(CGFloat r, CGFloat g, CGFloat b, CGFloat a)
{
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat comps[] = {r, g, b, a};
CGColorRef color = CGColorCreate(rgb, comps);
CGColorSpaceRelease(rgb);
return color;
}
但是,一旦我尝试使用此方法更改背景颜色,垂直线仍然会被消除。我在这里缺少什么?
如有任何想法,请提前表示感谢!
I'm attempting to create a grid-style table using the UITableView by overwriting vertical lines on top of the standard horizontal lines provided by default in a UITableView. I am adapting my code to the example helpfully provided from this blog: http://www.iphonedevx.com/?p=153.
First, the code which draws the vertical lines (same color as horizontal lines) is implemented in a file separate from the Table View Controller, called "MyTableCell.m":
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Use the same color and width as the default cell separator for now
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.25);
for (int i = 0; i < [columns count]; i++) {
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}
CGContextStrokePath(ctx);
[super drawRect:rect];
}
Next, in the Table View Controller tableView method, we call [cell addColumn:50] to draw a vertical line 50 pixels to the left of the left-hand side of the view boundary:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:50];
label.tag = LABEL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:@"%d", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(60.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:120];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text = [NSString stringWithFormat:@"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
return cell;
}
I am attempting to change the background of the entire view from white (the default) to black. I attempt to do this by setting the self.view.backgroundColor to black in the viewDidLoad method of the Table View Controller:
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
}
However, when I do this, the vertical lines disappear...
I think somehow setting the backgroundColor in viewDidLoad changes the CurrentContext by the time we get to the drawRect: method, but I don't know how to adjust for that. I've tried setting the background color via CGContextSetFillColorWithColor() inside drawRect: like this:
GContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, tableBackgroundColor());
CGContextFillRect(ctx, self.bounds);
where tableBackgroundColor() is black like this:
CGColorRef tableBackgroundColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceRGBColor(0.0, 0.0, 0.0, 1.0); // black
}
return c;
}
CGColorRef CreateDeviceRGBColor(CGFloat r, CGFloat g, CGFloat b, CGFloat a)
{
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat comps[] = {r, g, b, a};
CGColorRef color = CGColorCreate(rgb, comps);
CGColorSpaceRelease(rgb);
return color;
}
However, as soon as I attempt to change the background color using this method, the vertical lines still get wiped out. What am I missing here?
Any ideas are much appreciated in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来很奇怪,但也许drawRect:的默认实现会绘制背景颜色。尝试将
[super drawRect:rect]
移动到开头。It seems odd, but perhaps the default implementation of drawRect: draws the background colour. Try moving
[super drawRect:rect]
to the start.您确定它们正在消失,或者您看不到黑色背景上的浅灰色线条吗?
在drawRect:中我更改
为
在viewDidLoad中,我使用了
[UIColor YellowColor];
并且线条显示得很好。 (我还添加了对[super viewDidLoad]
的调用,但删除它没有任何区别。)Are you sure they're disappearing, or that you can't see a light gray line on top of a black background?
In drawRect: I changed
to
And in viewDidLoad, I used
[UIColor yellowColor];
and the lines appear just fine. (I also included a call to[super viewDidLoad]
, but removing it didn't make any difference.)