自定义cell背景和选中时颜色

来源:互联网 发布:c语言定义函数并调用 编辑:程序博客网 时间:2024/04/28 05:41

  下面两句代码即可

//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;

}

以上方法是在自定义cell的.m里写的 仅供参考 看实际情况而定

0 0