UITableViewCell左侧颜色

时间:2011-02-23 15:26:45

标签: ios uitableview ios4

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

我目前在表中有颜色,但就是这样。这是我的代码:

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

最好的问候,
保罗佩伦

2 个答案:

答案 0 :(得分:1)

我最终使用NR4TR建议的“CoreGraphics”解决了它。

我创建了一个新的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);
}

答案 1 :(得分:0)

最快最简单的方法是使用第一个和最后一个单元格的图像。

有一种更复杂的方式 - 借助Core Graphics

绘图

编辑:您的案例只是rectangle加上椭圆的引文(或只是pie slice)。

相关问题