在iPhone OS 3.0及更高版本中UITableViewCell现在有一个backgroundColor属性,使这非常容易(特别是与[UIColor colorWithPatternImage:]初始化程序结合使用)。但我会在这里为需要它的人留下2.0版的答案......
它比实际应该更难。以下是我必须这样做时的方法:
您需要将UITableViewCell的backgroundView属性设置为自定义UIView,以适当的颜色绘制边框和背景本身。此视图需要能够以4种不同的模式绘制边框,在一个部分中的第一个单元格的顶部倒圆,在截面的最后一个单元格的底部倒圆,在截面中间的单元格没有圆角,并在包含一个单元格的部分的所有4个角上舍入。
不幸的是我无法弄清楚如何自动设置这个模式,所以我不得不在UITableViewDataSource的-cellForRowAtIndexPath方法中设置它。
这是一个真正的PITA,但我已经向Apple工程师证实,这是目前唯一的方法。
更新这是自定义bg视图的代码。有一个绘图错误,使圆角看起来有点滑稽,但我有机会修复它之前我们转移到不同的设计并废弃自定义背景。这仍然可能对你非常有帮助:CustomCellBackgroundView.hCreatedbyMikeAkerson11/21/08.//Copyright__MyCompanyName__.Allrightsreserved.//#importtypedefenum{
CustomCellBackgroundViewPositionTop,
CustomCellBackgroundViewPositionMiddle,
CustomCellBackgroundViewPositionBottom,
CustomCellBackgroundViewPositionSingle}CustomCellBackgroundViewPosition;@interfaceCustomCellBackgroundView:UIView{
UIColor*borderColor;
UIColor*fillColor;
CustomCellBackgroundViewPositionposition;}
@property(nonatomic,retain)UIColor*borderColor,*fillColor;
@property(nonatomic)CustomCellBackgroundViewPositionposition;@endCustomCellBackgroundView.mCreatedbyMikeAkerson11/21/08.//Copyright__MyCompanyName__.Allrightsreserved.//#import"CustomCellBackgroundView.h"staticvoidaddRoundedRectToPath(CGContextRefcontext,CGRectrect,
floatovalWidth,floatovalHeight);@implementationCustomCellBackgroundView@synthesizeborderColor,fillColor,position;-(BOOL)isOpaque{
returnNO;}-(id)initWithFrame:(CGRect)frame{
if(self=[superinitWithFrame:frame]){
//Initializationcode
}
returnself;}-(void)drawRect:(CGRect)rect{
//Drawingcode
CGContextRefc=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c,[fillColorCGColor]);
CGContextSetStrokeColorWithColor(c,[borderColorCGColor]);
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));
}elseif(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));
}elseif(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;//noneedtobotherdrawingroundedcorners,sowereturn
}
//Atthispointthecliprectissettoonlydrawtheappropriate
//corners,sowefillandstrokearoundedrecttakingtheentirerect
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{
[borderColorrelease];
[fillColorrelease];
[superdealloc];}@endstaticvoidaddRoundedRectToPath(CGContextRefcontext,CGRectrect,
floatovalWidth,floatovalHeight){
floatfw,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}