2011-03-24 73 views
1

我嘗試了各種方法,但沒有成功。 其分組的UITableView。想要更改所選單元格的背景顏色。如何在分組的UITableView上設置選定的背景色

我試着創建一個視圖,設置背景顏色並將其設置爲cell.selectedBackgroundView。它可以改變顏色,但部分的圓角會丟失。

回答

4

您可以創建4個不同的圖像,頂部爲1,底部爲1,中間爲1,頂部/底部爲1(在所有4個角上爲圓形)。然後將背景視圖設置爲您的自定義圖像,具體取決於表格中的位置。另外,如果你想使用一個視圖,這裏的舍入只有特定角落的自定義視圖:

.H

#import <UIKit/UIKit.h> 

enum { 
    RoundedCornerNone  = 0, 
    RoundedCornerUpperRight = 1 << 0, 
    RoundedCornerLowerRight = 1 << 1, 
    RoundedCornerLowerLeft = 1 << 2, 
    RoundedCornerUpperLeft = 1 << 3 
}; 
typedef NSUInteger RoundedCornerOptions; 

@interface PartiallyRoundedView : UIView 

@property (nonatomic, assign) RoundedCornerOptions roundedCorners; 

@end 

.M

#import "PartiallyRoundedView.h" 


@implementation PartiallyRoundedView 

@synthesize roundedCorners; 

- (id)initWithFrame:(CGRect)frame 
{ 
    if ((self = [super initWithFrame:frame])) { 
    } 
    return self; 
} 

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

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 1.0); 
    CGContextBeginPath(context); 
    CGContextSetRGBStrokeColor(context, .6, .6, .6, 1); 
    CGContextSetRGBFillColor(context, .968, .968, .968, 1); 

    CGContextMoveToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y); 
    CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y); //Draw top line 

    if (self.roundedCorners >=8) { //Round upper-left corner 
     CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, 
         -M_PI/2, M_PI, 1); 

     self.roundedCorners-=8; 
    } 
    else { 
     CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y); 
     CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + radius); 
    } 

    CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius); //Draw left line 

    if (self.roundedCorners >=4) { //Round lower-left corner 
     CGContextAddArc(context, rect.origin.x + radius , rect.origin.y + rect.size.height - radius, 
         radius, M_PI, M_PI/2, 1); 

     self.roundedCorners-=4; 
    } 
    else { 
     CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height); 
     CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y + rect.size.height); 
    } 

    CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height); //Draw bottom line 

    if (self.roundedCorners >=2) { //Round lower-right corner 
     CGContextAddArc(context, rect.origin.x + rect.size.width - radius , 
         rect.origin.y + rect.size.height - radius, radius, M_PI/2, 0.0f, 1); 

     self.roundedCorners-=2; 
    } 
    else { 
     CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); 
     CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height - radius); 
    } 

    CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius); //Draw right line 

    if (self.roundedCorners ==1) { //Round upper-right corner 
     CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, 
         radius, 0.0f, -M_PI/2, 1); 
    } 
    else { 
     CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y); 
     CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y); 
    } 


    CGContextClosePath(context); 
    CGContextDrawPath(context, kCGPathFillStroke); 
    } 


    - (void)dealloc { 
     [super dealloc]; 
    } 


    @end 

您可以創建這個觀點的一個實例(你需要添加一點來填補你想要的顏色)。根據您是第一個,最後一個,中間還是第一個和最後一個單元,只需傳入正確的圓角四捨五入。

+0

我會標記它的答案。我沒有最終這樣做,似乎太複雜。我能夠通過不同的方式設置表格樣式。令人驚訝的是它必須是這個複雜的。這是最完整的答案。 – David 2012-02-21 19:27:28

0

如果你只是想改變選擇的顏色,看看這個建議: UITableView Cell selected Color? 郵政此在的cellForRowAtIndexPath法:

UIView *bgColorView = [[UIView alloc] init]; 
[bgColorView setBackgroundColor:[UIColor redColor]]; 
bgColorView.layer.cornerRadius = 10; 
[cell setSelectedBackgroundView:bgColorView]; 
[bgColorView release]; 

,不要忘記導入QuartzCore。適用於我的作品(iOS 5)

+0

對於分組表格,所有單元格的所有拐角都不是圓角。如果它在一個部分中,則所有四個角都是圓角的。如果它是一個部分中的第一個單元格,則只有頂部的圓角是圓角的。如果它在一個部分的底部單元格中,只有底部的角部是圓形的。如果它位於某個部分的中間,則不會有任何角落變圓。 – David 2012-02-21 19:25:17

2

如果要使用Core Graphics(不使用圖像)實現此功能,請給這個鏡頭一個鏡頭。我的方法採用與所選答案相同的概念,但使用簡單的if/else(或switch:case,具體取決於數據源大小)爲分組的UITableViewCell繪製正確的背景視圖。下面的代碼只需要包含在您的實現文件:

.M

UIView *bgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-24, 69)]; 
    bgColorView.backgroundColor = [UIColor magentaColor]; 
    UIBezierPath *maskPath; 
    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = bgColorView.bounds; 

    if (row == 0) { 
     maskPath = [UIBezierPath bezierPathWithRoundedRect:bgColorView.bounds 
             byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) 
              cornerRadii:CGSizeMake(8.0, 8.0)]; 
     maskLayer.frame = CGRectOffset(bgColorView.bounds, 1, 0); 
     if ([self.tableView numberOfRowsInSection:1] == 1) { 
     maskPath = [UIBezierPath bezierPathWithRoundedRect:bgColorView.bounds 
             byRoundingCorners:(UIRectCornerAllCorners) 
               cornerRadii:CGSizeMake(8.0, 8.0)]; 
     } 
    } else if (row >= [self.tableView numberOfRowsInSection:1]-1) { 
     maskPath = [UIBezierPath bezierPathWithRoundedRect:bgColorView.bounds 
             byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) 
              cornerRadii:CGSizeMake(8.0, 8.0)]; 
     maskLayer.frame = CGRectOffset(bgColorView.bounds, 1, 0); 
    } else { 
     maskPath = [UIBezierPath bezierPathWithRoundedRect:bgColorView.bounds 
             byRoundingCorners:nil 
              cornerRadii:CGSizeMake(0.0, 0.0)]; 
    } 

    maskLayer.path = maskPath.CGPath; 

    bgColorView.layer.mask = maskLayer; 
    cell.selectedBackgroundView = bgColorView; 

如果你已經使用可重複使用的電池,繪圖應該只爲每種類型出現一次。

希望這可以幫助有人絆倒到這個線程... 乾杯!

+0

+1,這實際上是一個更好的答案... – tpow 2013-06-17 21:16:32

相關問題