簡體   English   中英

如何使用分組樣式設置UITableView邊框

[英]How can I set the UITableView border with a grouped style

有誰知道是否可以替換分組表視圖單元格上的灰色邊框

與此帖相反: 在iPhone UITableView單元格中更改邊框顏色(未分組)

不幸的是,這並不容易,但你不必訴諸圖像。 這是我修改過的一個類 - 它來自如何自定義分組表視圖單元格的背景/邊框顏色? ,但我添加了在實心填充顏色或漸變之間切換的功能,以及使用藍色選擇模式的屬性,因此如果您支持選擇,則可以將其用作單元selectedBackgroundView。

模式:1.useBlackGradient或useWhiteGradient使用內置漸變2.gradientStartColor / gradientEndColor屬性來設置你自己的3.useBlueSelectionGradient = YES,使用藍色突出顯示將其置於選擇模式(與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
}

您需要使用所需的邊框創建整個tableviewcell。 這意味着你必須創建頂部,底部和中間單元格的圖片。 這個頁面上有一些小教程: http//cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM