gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:13:49 25 4
gpt4 key购买 nike

我有一个自定义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);
}

最佳答案

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath中,您可以调用[cell setNeedsDisplay];

但是,如果只需要一些简单的颜色和阴影,为什么不使用[cell setBackgroundColor:[UIColor yourColorXY]];

编辑:
就像郭陆川所说,是经常被称为drawRect:的性能杀手。
只需尝试简单;)

NSIndexPath *selectedCell; //Declare this in your .h

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
if (selectedCell.section==indexPath.section && selectedCell.row==indexPath.row) {
[cell.contentView setBackgroundColor:[UIColor whiteColor]];
[cell.textLabel setText:@"The Choosen One"];
} else {
[cell.contentView setBackgroundColor:[UIColor lightGrayColor]];
[cell.textLabel setText:@"FOO"];
}
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedCell=indexPath;
[tableView reloadData];
}

对于阴影,您可以玩玩..
#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];

关于ios - 如何刷新自定义绘制单元格的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14604162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com