为 tableview (UITableView) 绘制一个自定义单元格,更改颜色和分隔符颜色和宽度
Posted
技术标签:
【中文标题】为 tableview (UITableView) 绘制一个自定义单元格,更改颜色和分隔符颜色和宽度【英文标题】:Draw a Custom cell for tableview (UITableView), with changed colors and separator color and width 【发布时间】:2009-11-23 17:51:14 【问题描述】:我想绘制具有分组样式的 UITableViewCell 的背景。我的问题是我无法调用-(void)drawRect:(CGRect)rect
,或者我认为应该以编程方式调用它......
我已从以下链接获取代码。
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
但问题是我的 drawRect 没有被自动调用...
我就是这样做的。
CustomCellBackgroundView *custView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectMake(0,0,320,44)];
[cell setBackgroundView:custView];
[custView release];
这样做会给我透明的单元格。
【问题讨论】:
【参考方案1】:如果您的目标是 iPhone OS 3.0+,您需要做的就是:
cell.backgroundColor = [UIColor redColor];
当然,替换你真正想要的任何 UIColor。背景颜色将自动裁剪到分组表视图单元格的圆角矩形边界。
【讨论】:
【参考方案2】:大家好,
我已经让这段代码工作了......
只是这样做.....
在 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];
在你的 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;
记住不要在 (cell==nil) 块中这样做,并特别感谢所有在
How to customize the background/border colors of a grouped table view cell?
【讨论】:
【参考方案3】:我必须同意 Sbrocket。
您需要两行代码。如果要为行分隔符和边框使用自定义颜色,则为四行。
// 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];
为单元格背景和行分隔符添加您喜欢的任何颜色的 RGB 值。
再简单不过了。
【讨论】:
请尝试在 2.x 中执行此操作并保持 tableStyle 分组,然后查看您的是否更容易...... ;) 您到底为什么要开发和维护 2.x 代码?? 您正在承担所有开发难题,而不是将升级负担转移给您的用户。作为开发人员,这会花费你的钱。很坏。客户端/服务器开发的部分问题。 Chrome 操作系统规则! ;)以上是关于为 tableview (UITableView) 绘制一个自定义单元格,更改颜色和分隔符颜色和宽度的主要内容,如果未能解决你的问题,请参考以下文章
UITableView 的子视图与 tableView 的高度不同
iOS tableView 中 UITableView中UITableViewStyleGrouped和UITableViewStylePlain的区别
let tableView = UITableView()与let tableView之间的区别:UITableView!这两个语句
Swift 中的 UITableView 无法识别 UINavigationBar,填充已关闭,部分 tableview 被隐藏