2011-02-23 74 views
1

我想设置一个颜色到我的UITableViewCell的最左侧,但我希望它落在单元格的边界。 因为我使用UITableViewStyleGrouped UITableView我想颜色有第一个和最后一个单元格的圆角。我怎样才能做到这一点?UITableViewCell左侧颜色

我目前有表中的颜色,但多数民众赞成在它。这是我的代码:

UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, cell.frame.size.height)]; 
[theView setBackgroundColor:[UIColor purpleColor]]; 

[cell.contentView addSubview:theView]; 

[[cell textLabel] setText:@"Some row"]; 

这是结果:
enter image description here

最好的问候,
保罗Peelen

回答

0

最快,最简单的方法是使用图像的第一个和最后细胞。

有更多的复杂的方式 - 与Core Graphics

编辑的帮助下绘制:你的情况只是一个rectangle加上椭圆形(或只是一个pie slice)的报价者。

1

我使用“CoreGraphics”作为NR4TR的建议,最终解决了这个问题。

我创建了一个新的UIView并重写了drawRect函数。

这是结果代码:

- (void)drawRect:(CGRect)rect { 
    float radius = 10.0f; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    rect = CGRectInset(rect, 0.0f, 0.0f); 

    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect)); 
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect)); 


    if (bottom) 
    { 
     CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect) - radius, radius, M_PI/2, M_PI, 0); 
    } 
    else { 
     CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect)); 
    } 

    CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect)); 

    if (top) 
    { 
     CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect) + radius, radius, M_PI, 3 * M_PI/2, 0); 
    } 
    else { 
     CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect)); 
    } 

    CGContextSetFillColorWithColor(context, theColor.CGColor); 
    CGContextClosePath(context); 
    CGContextFillPath(context); 
} 
+0

你最好的解决方案更好地加入到与标签_Solution_比邮寄回答自己的问题你的答案。只是一个stackoverflow的问题:) – knuku 2011-02-25 11:52:26