2010-09-28 58 views
1

我试图通过在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; 
} 

然而,当我试图改变使用这种方法的背景颜色,垂直线条仍然被消除。我在这里错过了什么?

任何想法都提前非常感谢!

+0

aatlo moto code naa apaay。 baggha – 2013-06-12 09:34:46

回答

0

看起来很奇怪,但也许drawRect的默认实现:绘制背景颜色。尝试移动[super drawRect:rect]以开始。

+0

无法将[super drawRect:rect]移动到drawRect方法的开头 – Sly 2010-09-29 23:16:52

0

您确定它们正在消失,或者您看不到黑色背景上的浅灰色线条?

在的drawRect:我改变

CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0); 
CGContextSetLineWidth(ctx, 0.25); 

CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0); 
CGContextSetLineWidth(ctx, 10.0); 

而在viewDidLoad中,我用[UIColor yellowColor];和线出现就好了。 (我还包括致电[super viewDidLoad],但删除它没有任何区别。)

+0

对,谢谢Robot K.我可以复制它。我注意到,在黄色背景下,垂直线的颜色略有改变(它应该与水平台线颜色相同)。看起来,垂直线以某种方式在背景中绘制,在视图背景颜色后面绘制... – Sly 2010-09-29 23:18:30

相关问题