如何设置自定义cell 选中 时的 背景 颜色

2025-04-14 03:07:11
推荐回答(1个)
回答1:

下面两句代码即可
//cell颜色设置为白色
self.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageWithColor:[UIColor whiteColor]]];
//cell选中的颜色是淡蓝色
self.selectedBackgroundView=[[UIImageView alloc] initWithImage:[UIImage imageWithColor:UIColorFromRGB(0x81b9ea)]];
说明:
// 其中的【UIColorFromRGB】是宏
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// imageWithColor是【UIImage】的类别的扩充 (根据颜色生成一个图片)
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}