Iphone 图片

本文将指导你如何通过设置UITableViewCell的backgroundView属性来自定义分组表视图的背景颜色与边框样式,包括顶部、底部、中间位置的特殊处理。同时提供了一个自定义视图的实现代码,帮助你解决在UITableView中实现个性化分组背景的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何自定义分组表视图/边框颜色的背景您需要设置UITableViewCell的backgroundView属性自定义UIView,吸引了在适当的边界和背景颜色的本身。 这种观点必须能够吸纳4种不同的模式the边界,圆形至于有关第一次在一节细胞顶部,为在最后一节细胞bottom,因为在对一节middle cells no圆角圆角落,并四舍五入为第一个细胞都含有4个角落。



不幸的是我无法弄清楚如何使这种模式下自动设置,所以我不得不设在UITableViewDataSource的- cellForRowAtIndexPath它的方法。



这是一个真正的太平洋岛屿电信协会,但我已经与苹果公司的工程师证实,这是目前唯一的办法。



更新下面是自定义视图代码为保函。 有一个绘图错误,使圆角看起来有点滑稽,但我们转移到一个不同的设计和取消了自定义的背景前,我有机会来解决它。 不过这可能会很对你有帮助:



//

// 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]);

if (position == CustomCellBackgroundViewPositionTop) {

CGContextFillRect(c, CGRectMake(0.0f, rect.size.height - 10.0f, rect.size.width, 10.0f));

CGContextBeginPath(c);

CGContextMoveToPoint(c, 0.0f, rect.size.height - 10.0f);

CGContextAddLineToPoint(c, 0.0f, rect.size.height);

CGContextAddLineToPoint(c, rect.size.width, rect.size.height);

CGContextAddLineToPoint(c, rect.size.width, rect.size.height - 10.0f);

CGContextStrokePath(c);

CGContextClipToRect(c, CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height - 10.0f));

} else if (position == CustomCellBackgroundViewPositionBottom) {

CGContextFillRect(c, CGRectMake(0.0f, 0.0f, rect.size.width, 10.0f));

CGContextBeginPath(c);

CGContextMoveToPoint(c, 0.0f, 10.0f);

CGContextAddLineToPoint(c, 0.0f, 0.0f);

CGContextStrokePath(c);

CGContextBeginPath(c);

CGContextMoveToPoint(c, rect.size.width, 0.0f);

CGContextAddLineToPoint(c, rect.size.width, 10.0f);

CGContextStrokePath(c);

CGContextClipToRect(c, CGRectMake(0.0f, 10.0f, rect.size.width, rect.size.height));

} else if (position == CustomCellBackgroundViewPositionMiddle) {

CGContextFillRect(c, rect);

CGContextBeginPath(c);

CGContextMoveToPoint(c, 0.0f, 0.0f);

CGContextAddLineToPoint(c, 0.0f, rect.size.height);

CGContextAddLineToPoint(c, rect.size.width, rect.size.height);

CGContextAddLineToPoint(c, rect.size.width, 0.0f);

CGContextStrokePath(c);

return; // no need to bother drawing rounded corners, so we return

}

// At this point the clip rect is set to only draw the appropriate

// corners, so we fill and stroke a rounded rect taking the entire rect

CGContextBeginPath(c);

addRoundedRectToPath(c, rect, 10.0f, 10.0f);

CGContextFillPath(c);

CGContextSetLineWidth(c, 1);

CGContextBeginPath(c);

addRoundedRectToPath(c, rect, 10.0f, 10.0f);

CGContextStrokePath(c);

}



- (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

}




首先感谢所有为这个代码。 我已经在这个函数绘图改变,以消除一些角落绘制问题。



-(void)drawRect:(CGRect)rect

{

// Drawing code

CGContextRef c = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(c, [fillColor CGColor]);

CGContextSetStrokeColorWithColor(c, [borderColor CGColor]);

CGContextSetLineWidth(c, 2);

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;

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值