2011-02-23 93 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