Iphone 如何使用分组样式设置UITableView边框

Iphone 如何使用分组样式设置UITableView边框,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,有人知道是否可以替换分组表视图单元格上的灰色边框吗 与本文相反:您需要创建一个带有所需边框的整个tableviewcell。这意味着您必须创建顶部、底部和中间单元格的图片。 本页有一个小教程:您需要使用所需的边框创建整个tableviewcell。这意味着您必须创建顶部、底部和中间单元格的图片。 本页有一个小教程:不幸的是,这并不容易,但你不必求助于图像。这是我修改过的一个类-它来自,但我添加了在实心填充颜色或渐变之间切换的功能,以及使用蓝色选择模式的属性,因此如果支持选择,您可以将其用作cel

有人知道是否可以替换分组表视图单元格上的灰色边框吗


与本文相反:

您需要创建一个带有所需边框的整个tableviewcell。这意味着您必须创建顶部、底部和中间单元格的图片。
本页有一个小教程:

您需要使用所需的边框创建整个tableviewcell。这意味着您必须创建顶部、底部和中间单元格的图片。
本页有一个小教程:

不幸的是,这并不容易,但你不必求助于图像。这是我修改过的一个类-它来自,但我添加了在实心填充颜色或渐变之间切换的功能,以及使用蓝色选择模式的属性,因此如果支持选择,您可以将其用作cell selectedBackgroundView

模式: 1.useBlackGradient或useWhiteGradient使用内置渐变 2.gradientStartColor/gradientEndColor属性设置您自己的颜色 3.UseBluseSelectionGradient=是,使用蓝色突出显示将其置于选择模式(与apple使用的相同)

以下是您如何使用它:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 9.0;

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.opaque = NO;
    cell.textLabel.textColor = [UIColor blackColor];


    if(!cell.backgroundView || ![cell.backgroundView isKindOfClass:[CustomCellBackgroundView class]])
    {
        cell.backgroundView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];
    }
    CustomCellBackgroundView *bgView = (CustomCellBackgroundView *)cell.backgroundView;
    NSInteger numRows = [self tableView:self.tableView numberOfRowsInSection:indexPath.section];
    if(indexPath.row == 0 && indexPath.row == numRows - 1)
    {
        bgView.position = CustomCellBackgroundViewPositionSingle;
    }
    else if (indexPath.row == 0) 
    {
        bgView.position = CustomCellBackgroundViewPositionTop;
    } 
    else if (indexPath.row != numRows - 1) 
    {
        bgView.position = CustomCellBackgroundViewPositionMiddle;
    } 
    else 
    {
        bgView.position = CustomCellBackgroundViewPositionBottom;
    }

    [bgView useWhiteGradient];
    bgView.borderColor = [UIColor clearColor];
    //bgView.fillColor = [UIColor greenColor];

    return cell;
}
以下是标题和实现:

//CustomCellBackgroundView.h
#import <UIKit/UIKit.h>

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

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

    UIColor *gradientStartColor;
    UIColor *gradientEndColor;

    BOOL useBlueSelectionGradient;
    CGGradientRef gradient;
    CGGradientRef selectGradient;
}

@property(nonatomic, retain) UIColor *borderColor, *fillColor;
@property(nonatomic, retain) UIColor *gradientStartColor, *gradientEndColor;
@property(nonatomic, assign) BOOL useBlueSelectionGradient;
@property(nonatomic) CustomCellBackgroundViewPosition position;


-(void)useBlackGradient;
-(void)useWhiteGradient;

@end



//CustomCellBackgroundView.m
#import "CustomCellBackgroundView.h"

#define ROUND_SIZE 10

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

@implementation CustomCellBackgroundView
@synthesize borderColor, fillColor, position, useBlueSelectionGradient;
@synthesize gradientStartColor, gradientEndColor;

- (BOOL) isOpaque {
    return NO;
}

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


- (void)createGradient
{
    if (gradient != NULL)
    {
        CGGradientRelease(gradient);
        gradient = NULL;
    }

    if(self.gradientStartColor && self.gradientEndColor)
    {
        const float* topColor = CGColorGetComponents([self.gradientStartColor CGColor]);
        const float* bottomColor = CGColorGetComponents([self.gradientEndColor CGColor]);

        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        CGFloat colors[]=
        {
            topColor[0],    topColor[1],    topColor[2],    topColor[3],
            bottomColor[0], bottomColor[1], bottomColor[2], bottomColor[3]
        };
        size_t numColors = sizeof(colors)/(sizeof(colors[0])*4);
        gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, numColors);
        CGColorSpaceRelease(rgb);
    }
}

- (void)setUseBlueSelectionGradient:(BOOL)newValue
{
    useBlueSelectionGradient = newValue;
    if(useBlueSelectionGradient && selectGradient == NULL)
    {
        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        CGFloat colors[] =
        {
            5.0 / 255.0, 140.0 / 255.0, 245.0 / 255.0, 1.00,
            1.0 / 255.0,  93.0 / 255.0, 230.0 / 255.0, 1.00,
        };
        size_t numColors = sizeof(colors)/(sizeof(colors[0])*4);
        selectGradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, numColors);
        CGColorSpaceRelease(rgb);
        [self setNeedsDisplay];
    }
    else if (!useBlueSelectionGradient && selectGradient != NULL)
    {
        CGGradientRelease(selectGradient);
        selectGradient = NULL;
        [self setNeedsDisplay];
    }
}

- (void)setGradientStartColor:(UIColor *)inColor
{
    if(inColor != gradientStartColor)
    {
        [gradientStartColor release];
        gradientStartColor = inColor;
        [gradientStartColor retain];
        [self createGradient];
    }
}

- (void)setGradientEndColor:(UIColor *)inColor
{
    if(inColor != gradientEndColor)
    {
        [gradientEndColor release];
        gradientEndColor = inColor;
        [gradientEndColor retain];
        [self createGradient];
    }
}

- (void) setPosition:(CustomCellBackgroundViewPosition)inPosition
{
    if(position != inPosition)
    {
        position = inPosition;
        [self setNeedsDisplay];
    }
}

-(void)useBlackGradient
{
    self.gradientStartColor = [UIColor colorWithRed:0.154 green:0.154 blue:0.154 alpha:1.0];
    self.gradientEndColor = [UIColor colorWithRed:0.307 green:0.307 blue:0.307 alpha:1.0];
}

-(void)useWhiteGradient
{
    self.gradientStartColor = [UIColor colorWithRed:0.864 green:0.864 blue:0.864 alpha:1.0];
    self.gradientEndColor = [UIColor colorWithRed:0.956 green:0.956 blue:0.956 alpha:1.0];
}

-(void)drawRect:(CGRect)rect 
{
    // Drawing code

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(c, [fillColor CGColor]);
    CGContextSetStrokeColorWithColor(c, [borderColor CGColor]);
    CGContextSetLineWidth(c, 2);


    CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
    CGFloat miny = CGRectGetMinY(rect) , midy = CGRectGetMidY(rect) , maxy = CGRectGetMaxY(rect) ;
    if (position == CustomCellBackgroundViewPositionTop) 
    {
        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);
    } 
    else if (position == CustomCellBackgroundViewPositionBottom) 
    {
        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);
    } 
    else if (position == CustomCellBackgroundViewPositionMiddle) 
    {
        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);
    }
    else if (position == CustomCellBackgroundViewPositionSingle)
    {
        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);
    }
    else 
    {
        return;
    }


    // Close the path
    CGContextClosePath(c);  

    if(selectGradient != NULL || gradient != NULL)
    {
        CGContextSaveGState(c);
        CGContextClip(c);
        CGContextDrawLinearGradient(c, 
                                    selectGradient != NULL ? selectGradient : gradient, 
                                    CGPointMake(minx,miny), 
                                    CGPointMake(minx,maxy), 
                                    kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
        CGContextRestoreGState(c);
    }
    else 
    {
        CGContextDrawPath(c, kCGPathFillStroke);
    }
}

- (void)dealloc 
{
    if (selectGradient != NULL)
    {
        CGGradientRelease(selectGradient);
        selectGradient = NULL;
    }
    if (gradient != NULL)
    {
        CGGradientRelease(gradient);
        gradient = NULL;
    }

    [gradientStartColor release];
    [gradientEndColor release];
    [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
}
//CustomCellBackgroundView.h
#进口
类型定义枚举{
CustomCellBackgroundViewPositionTop,
CustomCellBackgroundViewPositionMiddle,
CustomCellBackgroundViewPositionBottom,
CustomCellBackgroundViewPositionSingle
}CustomCellBackgroundViewPosition;
@接口CustomCellBackgroundView:UIView{
UIColor*边框颜色;
UIColor*fillColor;
CustomCellBackgroundViewPosition;
UIColor*渐变开始颜色;
UIColor*gradientEndColor;
布尔蓝选择梯度;
梯度梯度;
CGGradientRef选择梯度;
}
@属性(非原子,保留)UIColor*borderColor,*fillColor;
@属性(非原子,保留)UIColor*gradientStartColor,*gradientEndColor;
@属性(非原子,赋值)布尔蓝选择梯度;
@属性(非原子)CustomCellBackgroundViewPosition;
-(空)坡度;
-(无效)使用白色梯度;
@结束
//CustomCellBackgroundView.m
#导入“CustomCellBackgroundView.h”
#定义圆形尺寸10
静态void addRoundedRectPath(CGContextRef上下文,CGRect rect,
浮动椭圆宽度、浮动椭圆高度);
@实现CustomCellBackgroundView
@合成边框颜色,填充颜色,位置,使用蓝色选择梯度;
@合成gradientStartColor、gradientEndColor;
-(布尔)等水量{
返回否;
}
-(id)initWithFrame:(CGRect)帧{
if(self=[super initWithFrame:frame]){
//初始化代码
selectGradient=NULL;
梯度=零;
}
回归自我;
}
-(void)创建渐变
{
如果(梯度!=NULL)
{
CGGradientRelease(梯度);
梯度=零;
}
if(self.gradientStartColor&&self.gradientEndColor)
{
常量浮点*topColor=CGColorGetComponents([self.gradientStartColor CGColor]);
常量浮点*bottomColor=CGColorGetComponents([self.gradientEndColor CGColor]);
CGColorSpaceRef rgb=CGColorSpaceCreateDeviceRGB();
CGFloat颜色[]=
{
topColor[0],topColor[1],topColor[2],topColor[3],
bottomColor[0],bottomColor[1],bottomColor[2],bottomColor[3]
};
size_t numColors=sizeof(colors)/(sizeof(colors[0])*4;
gradient=CGGradientCreateWithColorComponents(rgb、颜色、NULL、numColor);
CGB酶(rgb);
}
}
-(void)setUseBluseSelectionGradient:(BOOL)newValue
{
UseBluseElectionGradient=新值;
如果(使用BlueSelectionGradient&&selectGradient==NULL)
{
CGColorSpaceRef rgb=CGColorSpaceCreateDeviceRGB();
CGFloat颜色[]=
{
5.0 / 255.0, 140.0 / 255.0, 245.0 / 255.0, 1.00,
1.0 / 255.0,  93.0 / 255.0, 230.0 / 255.0, 1.00,
};
size_t numColors=sizeof(colors)/(sizeof(colors[0])*4;
选择Gradient=CGGradientCreateWithColorComponents(rgb、颜色、NULL、numColor);
CGB酶(rgb);
[自我设置需要显示];
}
如果(!UseBluseSelectionGradient&&selectGradient!=NULL),则为else
{
CGGradientRelease(选择梯度);
selectGradient=NULL;
[自我设置需要显示];
}
}
-(无效)setGradientStartColor:(UIColor*)inColor
{
如果(输入颜色!=渐变开始颜色)
{
[gradientStartColor release];
gradientStartColor=inColor;
[gradientStartColor保留];
[自创梯度];
}
}
-(void)setGradientEndColor:(UIColor*)inColor
{
if(inColor!=渐变色)
{
[gradientEndColor release];
gradientEndColor=inColor;
[gradientEndColor保留];
[自创梯度];
}
}
-(无效)设置位置:(CustomCellBackgroundViewPosition)inPosition
{
如果(位置!=inPosition)
{
位置=位置;
[自我设置需要显示];
}
}
-(无效)使用黑色渐变
{
self.gradientStartColor=[UIColor color withred:0.154绿色:0.154蓝色:0.154 alpha:1.0];
self.gradientEndColor=[UIColor COLOR WITHRED:0.307绿色:0.307蓝色:0.307阿尔法:1.0];
}
-(无效)使用白色渐变
{
self.gradientStartColor=[UIColor color with red:0.864 green:0.864 blue:0.864 alpha:1.0];
self.gradientEndColor=[UIColor COLOR WITHRED:0.956绿色:0.956蓝色:0.956阿尔法:1.0];
}
-(void)drawRect:(CGRect)rect
{
//绘图代码
CGContextRef c=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c[fillColor CGColor]);
CGContextSetStrokeColorWithColor(c[borderColor CGColor]);
CGContextSetLineWidth(c,2);
CGFloat minx=CGRectGetMinX(rect),midx=CGRectGetMidX(rect),maxx=CGRectGetMaxX(rect);
CGFloat miny=CGRectGetMinY(rect),midy=CGRectGetMidY(rect),maxy=CGRectGetMaxY(rect);
如果(位置==CustomCellBackgroundViewPositionTop)
{
惯性矩