简体   繁体   English

为tableview(UITableView)绘制自定义单元格,更改颜色和分隔符颜色和宽度

[英]Draw a Custom cell for tableview (UITableView), with changed colors and separator color and width

I want to draw the background of a UITableViewCell which has a grouped style. 我想绘制一个具有分组样式的UITableViewCell的背景。 The problem with me is I am not able to call the -(void)drawRect:(CGRect)rect or I think it should be called programmatically... 我的问题是我无法调用-(void)drawRect:(CGRect)rect或我认为它应该以编程方式调用...

I have taken code from following link . 我从以下链接中获取了代码。

How to customize the background/border colors of a grouped table view cell? 如何自定义分组表视图单元格的背景/边框颜色?

//
//  CustomCellBackgroundView.h
//
//  Created by Mike Akers on 11/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum  {
    CustomCellBackgroundViewPositionTop, 
    CustomCellBackgroundViewPositionMiddle, 
    CustomCellBackgroundViewPositionBottom,
    CustomCellBackgroundViewPositionSingle
} CustomCellBackgroundViewPosition;

@interface CustomCellBackgroundView : UIView {
    UIColor *borderColor;
    UIColor *fillColor;
    CustomCellBackgroundViewPosition position;
}

    @property(nonatomic, retain) UIColor *borderColor, *fillColor;
    @property(nonatomic) CustomCellBackgroundViewPosition position;
@end

//
//  CustomCellBackgroundView.m
//
//  Created by Mike Akers on 11/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "CustomCellBackgroundView.h"

static void addRoundedRectToPath(CGContextRef context, CGRect rect,
                                 float ovalWidth,float     ovalHeight);

@implementation CustomCellBackgroundView
@synthesize borderColor, fillColor, position;

- (BOOL) isOpaque {
    return NO;
}

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

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(c, [fillColor CGColor]);
    CGContextSetStrokeColorWithColor(c, [borderColor CGColor]);
    CGContextSetLineWidth(c, 2.0);

    if (position == CustomCellBackgroundViewPositionTop) {

        CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
        CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
        minx = minx + 1;
        miny = miny + 1;

        maxx = maxx - 1;
        maxy = maxy ;

        CGContextMoveToPoint(c, minx, maxy);
        CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE);
        CGContextAddArcToPoint(c, maxx, miny, maxx, maxy, ROUND_SIZE);
        CGContextAddLineToPoint(c, maxx, maxy);

        // Close the path
        CGContextClosePath(c);
        // Fill & stroke the path
        CGContextDrawPath(c, kCGPathFillStroke);                
        return;
    }

    else if (position == CustomCellBackgroundViewPositionBottom) {

        CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
        CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
        minx = minx + 1;
        miny = miny ;

        maxx = maxx - 1;
        maxy = maxy - 1;

        CGContextMoveToPoint(c, minx, miny);
        CGContextAddArcToPoint(c, minx, maxy, midx, maxy, ROUND_SIZE);
        CGContextAddArcToPoint(c, maxx, maxy, maxx, miny, ROUND_SIZE);
        CGContextAddLineToPoint(c, maxx, miny);
        // Close the path
        CGContextClosePath(c);
        // Fill & stroke the path
        CGContextDrawPath(c, kCGPathFillStroke);        
        return;
    } 
    else if (position == CustomCellBackgroundViewPositionMiddle) {
        CGFloat minx = CGRectGetMinX(rect) , maxx = CGRectGetMaxX(rect) ;
        CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
        minx = minx + 1;
        miny = miny ;

        maxx = maxx - 1;
        maxy = maxy ;

        CGContextMoveToPoint(c, minx, miny);
        CGContextAddLineToPoint(c, maxx, miny);
        CGContextAddLineToPoint(c, maxx, maxy);
        CGContextAddLineToPoint(c, minx, maxy);

        CGContextClosePath(c);
        // Fill & stroke the path
        CGContextDrawPath(c, kCGPathFillStroke);        
        return;
    }

    else if (position == CustomCellBackgroundViewPositionSingle)
    {
        CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
        CGFloat miny = CGRectGetMinY(rect) , midy = CGRectGetMidY(rect) , maxy = CGRectGetMaxY(rect) ;
        minx = minx + 1;
        miny = miny + 1;

        maxx = maxx - 1;
        maxy = maxy - 1;

        CGContextMoveToPoint(c, minx, midy);
        CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE);
        CGContextAddArcToPoint(c, maxx, miny, maxx, midy, ROUND_SIZE);
        CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, ROUND_SIZE);
        CGContextAddArcToPoint(c, minx, maxy, minx, midy, ROUND_SIZE);

        // Close the path
        CGContextClosePath(c);
        // Fill & stroke the path
        CGContextDrawPath(c, kCGPathFillStroke);                
        return;         
    }
}


- (void)dealloc {
    [borderColor release];
    [fillColor release];
    [super dealloc];
}


@end

static void addRoundedRectToPath(CGContextRef context, CGRect rect,
                                float ovalWidth,float     ovalHeight)

{
    float fw, fh;

    if (ovalWidth == 0 || ovalHeight == 0) {// 1
        CGContextAddRect(context, rect);
        return;
    }

    CGContextSaveGState(context);// 2

    CGContextTranslateCTM (context, CGRectGetMinX(rect),// 3
                           CGRectGetMinY(rect));
    CGContextScaleCTM (context, ovalWidth, ovalHeight);// 4
    fw = CGRectGetWidth (rect) / ovalWidth;// 5
    fh = CGRectGetHeight (rect) / ovalHeight;// 6

    CGContextMoveToPoint(context, fw, fh/2); // 7
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);// 8
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);// 9
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);// 10
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
    CGContextClosePath(context);// 12

    CGContextRestoreGState(context);// 13
}

but the problem is my drawRect is not getting called automatically... 但问题是我的drawRect没有被自动调用...

I am doing it like this. 我是这样做的。

CustomCellBackgroundView *custView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectMake(0,0,320,44)];
[cell setBackgroundView:custView];
[custView release];

and doing this gives me transparent cell. 这样做给了我透明的细胞。

If you are targeting iPhone OS 3.0+, all you need to do is this: 如果您的目标是iPhone OS 3.0+,那么您需要做的就是:

cell.backgroundColor = [UIColor redColor];

Substituting whatever UIColor you actually want, of course. 当然,用你真正想要的任何UIColor代替。 The background color will be clipped automatically to the rounded rectangle bounds of the grouped table view cell. 背景颜色将自动剪裁到分组表视图单元格的圆角矩形边界。

HI GUYS, 嗨,大家好,

I have got this code working.... 我有这个代码工作....

just did this..... 刚刚这样做.....

In CustomCellBackGroundView 在CustomCellBackGroundView中

- (BOOL) isOpaque {
    return NO;
}



- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setFillColor:[UIColor colorWithRed:0.07 green:.23 blue:.48 alpha:0.5]];
        [self setBorderColor:[UIColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:0.5]];
        [self setClearsContextBeforeDrawing:YES];
    }
    return self;
}

- (void)setNeedsDisplay{
    [self setNeedsDisplayInRect:self.frame];
}

and in ur UITableViewController class just do this..... 在你的UITableViewController类中就这样做.....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *MyIdentifier = @"My Identifier";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if(cell==nil){

        cell = [self reuseTableViewCellWithIdentifier:MyIdentifier indexPath:indexPath];

        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    }

    CustomCellBackgroundView *custview = [[CustomCellBackgroundView alloc] initWithFrame:CGRectMake(0, 0, 320, 35)];

    if(([tbleView numberOfRowsInSection:indexPath.section]-1) == 0){
        custview.position = CustomCellBackgroundViewPositionTop;
    }
    else if(indexPath.row == 0){
        custview.position = CustomCellBackgroundViewPositionTop;
    }
    else if (indexPath.row == ([tbleView numberOfRowsInSection:indexPath.section]-1)){
        custview.position  = CustomCellBackgroundViewPositionBottom;
    }
    else{
        custview.position = CustomCellBackgroundViewPositionMiddle;
    }
    [cell setBackgroundView:custview];
    [custview release];


    UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:101];



        [lbl setText:@"Hurray and thanks to all folks"];

    //[cell setNeedsDisplay];
    return cell;
}

remeber dont do this in (cell==nil) block and give special thanks to all folks at 请记住(cell == nil)阻止这一点并特别感谢所有人

How to customize the background/border colors of a grouped table view cell? 如何自定义分组表视图单元格的背景/边框颜色?

I have to agree with Sbrocket. 我不得不同意Sbrocket。

You need two lines of code. 你需要两行代码。 Four lines if you want to use a custom color for the line separator and border. 如果要为线条分隔符和边框使用自定义颜色,则为四行。

// For cell background
 cell.backgroundColor=[UIColor lightGrayColor];

// For tableView line separator (assuming IBOutlet UITableView* table;)
self.table.separatorColor=[UIColor redColor];

//If you need a custom color (I assume you do), then...
 cell.backgroundColor=[UIColor colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha];

Drop in the RGB value for any color you like for both the cell background and line separator. 为单元格背景和行分隔符选择您喜欢的任何颜色的RGB值。

Couldn't be easier. 不可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM