Ios 如何刷新自定义绘制单元格的颜色?
我有一个自定义UITableViewCell类,它实现了自定义绘制的设计(如您所见,单元具有阴影效果) 正如你们在下面看到的,我试图用白色来画第一个单元格,任何其他单元格都必须是灰色的Ios 如何刷新自定义绘制单元格的颜色?,ios,uitableview,drawrect,Ios,Uitableview,Drawrect,我有一个自定义UITableViewCell类,它实现了自定义绘制的设计(如您所见,单元具有阴影效果) 正如你们在下面看到的,我试图用白色来画第一个单元格,任何其他单元格都必须是灰色的 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 a
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
if (_rowNumber == 0)
CGColorRef lightColor = [UIColor whiteColor].CGColor;
else
CGColorRef lightColor = [UIColor grayColor].CGColor;
CGColorRef darkColor = _darkColor.CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.5].CGColor;
CGContextSetFillColorWithColor(context, whiteColor);
CGContextFillRect(context, _paperRect);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 7.0, shadowColor);
CGContextSetFillColorWithColor(context, lightColor);
CGContextFillRect(context, _coloredBoxRect);
CGContextRestoreGState(context);
}
问题是:当我使用dequeueReusableCellWithIdentifier时,方法“drawRect”只调用了3次(我的单元格高度为200),因此第四个单元格的颜色与第一个相同
有办法解决这个问题吗?
谢谢
更新
我已尝试从UITableViewController调用自定义重画方法,但出现“null”上下文错误
- (void)refreshColorOfRow:(int)row{
CGContextRef context = UIGraphicsGetCurrentContext();
if (row > 0)
_lightColor = [UIColor colorWithRed:200.0f/255.0f green:199.0f/255.0f blue:200.0f/255.0f alpha:1.0];
else
_lightColor = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0];
CGColorRef lightColor = _lightColor.CGColor;
CGContextSetFillColorWithColor(context, lightColor);
CGContextFillRect(context, _coloredBoxRect);
CGContextRestoreGState(context);
}
我假设您正在将
\u rowNumber
设置为UITableViewCell
子类的属性。
您应该在setter中调用setNeedsDisplay以强制重画:
-(void) setRowNumber:(int)value {
_rowNumber = value;
[self setNeedsDisplay];
}
我认为您可以使
第一个单元格
和其他单元格
不同的标识符
,例如第一个单元格
的Resuidentifier是@“first”
其他单元格
s resuidentifier是@“others”
,然后在自定义单元格中,您可以创建一个BOOL\u isFirstCell
来标记第一个单元格
,然后在-(void)drawRect:(CGRect)rect
方法中,您可以绘制两种类型的单元格使用如果其他
station
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
if (_isFirstCell)
CGColorRef lightColor = [UIColor whiteColor].CGColor;
else
CGColorRef lightColor = [UIColor grayColor].CGColor;
CGColorRef darkColor = _darkColor.CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.5].CGColor;
CGContextSetFillColorWithColor(context, whiteColor);
CGContextFillRect(context, _paperRect);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 7.0, shadowColor);
CGContextSetFillColorWithColor(context, lightColor);
CGContextFillRect(context, _coloredBoxRect);
CGContextRestoreGState(context);
}
在-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath
中,您可以调用[cell setNeedsDisplay]代码>
但是为什么不使用[cell-setBackgroundColor:[UIColor-yourColorXY]代码>如果你只想要一些简单的颜色和阴影
编辑:
正如郭绿川所说,是一个经常被称为drawRect:
的性能杀手。
试着简单一点;)
对于你可以玩弄的阴影
#import <QuartzCore/QuartzCore.h>
CALayer *layer=[cell layer]; //Or [tableView layer] or whatever you need
[layer setShadowColor:[[UIColor lightGrayColor] CGColor]];
[layer setShadowOffset:CGSizeMake(1, 1)];
[layer setShadowOpacity:1.0];
[layer setMasksToBounds:NO];
#导入
CALayer*层=[单元层]//或[tableView layer]或您需要的任何内容
[图层设置阴影颜色:[[UIColor lightGrayColor]CGColor];
[图层设置阴影偏移:CGSizeMake(1,1)];
[图层设置阴影不透明度:1.0];
[图层设置maskstobounds:否];
我不使用[cell-setBackgroundColor:[UIColor-yourColorXY]]因为我有点傻。感谢您的提前帮助^)@AndreyKaskaad-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath将被多次调用,并且您必须多次绘制,你的应用程序将非常无效我刚刚在我的answer@xapslock我刚刚发现,你的QuartzCore阴影也是性能杀手。我在真正的设备上测试它。。。所以要小心。是的,如果你不小心使用它,它是很棘手的。但是有一些表演技巧。。。view.layer.shadowPath=[UIBezierPath-bezierpath-withrect:view.bounds].CGPath;
#import <QuartzCore/QuartzCore.h>
CALayer *layer=[cell layer]; //Or [tableView layer] or whatever you need
[layer setShadowColor:[[UIColor lightGrayColor] CGColor]];
[layer setShadowOffset:CGSizeMake(1, 1)];
[layer setShadowOpacity:1.0];
[layer setMasksToBounds:NO];