我正在尝试通过覆盖 UITableView 中默认提供的标准水平线顶部的垂直线来使用 UITableView 创建一个网格样式表。我正在调整我的代码以适应此博客提供的示例:http ://www.iphonedevx.com/?p=153 。
首先,绘制垂直线(与水平线颜色相同)的代码在独立于 Table View Controller 的文件中实现,名为“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];
}
接下来,在 Table View Controller 的 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;
}
我正在尝试将整个视图的背景从白色(默认)更改为黑色。我尝试通过在 Table View Controller 的 viewDidLoad 方法中将 self.view.backgroundColor 设置为黑色来做到这一点:
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
}
但是,当我这样做时,垂直线消失了......
我认为在 viewDidLoad 中设置 backgroundColor 会在我们到达 drawRect: 方法时改变 CurrentContext,但我不知道如何调整。我尝试通过 CGContextSetFillColorWithColor() 在 drawRect: 中设置背景颜色,如下所示:
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;
}
但是,一旦我尝试使用这种方法更改背景颜色,垂直线仍然会被抹掉。我在这里想念什么?
任何想法都非常感谢提前!