Ios 如何刷新自定义绘制单元格的颜色?

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

我有一个自定义UITableViewCell类,它实现了自定义绘制的设计(如您所见,单元具有阴影效果) 正如你们在下面看到的,我试图用白色来画第一个单元格,任何其他单元格都必须是灰色的

- (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]]#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];