我试图通过在UITableView中默认提供的标准水平线上覆盖垂直线来使用UITableView创建网格样式表。我正在调整我的代码到本博客有用的示例中:http://www.iphonedevx.com/?p=153。在使用Core Graphics为网格添加垂直线时设置表格视图中的背景颜色
首先,其绘制垂直线(颜色相同水平线)的代码在文件从表视图控制器分开实现,称为“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方法,我们称之为[细胞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];
}
然而,当我这样做,垂直线条消失......
我想设置backgroundColor在viewDidLoad改变CurrentContext的时候,我们到了drawRect:方法,但我不知道如何调整。我试图通过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;
}
然而,当我试图改变使用这种方法的背景颜色,垂直线条仍然被消除。我在这里错过了什么?
任何想法都提前非常感谢!
aatlo moto code naa apaay。 baggha – 2013-06-12 09:34:46