I am trying to update the UITableViewCell view with a different type of theme.
I have a few themes but cannot get them to work correctly.
@interface CustomRowBackground : UIView {
CGColorRef topStroke;
CGColorRef lightColor;
CGColorRef darkColor;
CGColorRef bottomStroke; //also known as separator
CGColorRef borderColor;
}
- (id)initWithFrame:(CGRect)frame topStroke:(CGColorRef)top lightColor:(CGColorRef)light darkColor:(CGColorRef)dark bottomStroke:(CGColorRef)bottom borderColor:(CGColorRef)border;
@end
- (id)initWithFrame:(CGRect)frame topStroke:(CGColorRef)top lightColor:(CGColorRef)light darkColor:(CGColorRef)dark bottomStroke:(CGColorRef)bottom borderColor:(CGColorRef)border
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
topStroke = top;
lightColor = light;
darkColor = dark;
bottomStroke = bottom;
borderColor = border;
}
return self;
}
Now in my UIViewController, .h file, I extend the UIColor class with a helper method
@interface UIColor (BIExtras)
+(UIColor *)colorWithR:(CGFloat)red G:(CGFloat)green B:(CGFloat)blue A:(CGFloat)alpha;
@end
then in the .m I write this
@implementation UIColor (BIExtras)
+(UIColor *)colorWithR:(CGFloat)red G:(CGFloat)green B:(CGFloat)blue A:(CGFloat)alpha {
return [UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:alpha];
}
@end
then I try to set the desired gradient that I’m after for a particular row.
TaskDisplayCell *cell = (TaskDisplayCell *)[self.tableView dequeueReusableCellWithIdentifier:TaskCellIdentifier];
if (indexPath.row == 0) {
cell.backgroundView = [[CustomRowBackground alloc] initWithFrame:CGRectMake(0,0,self.tableView.bounds.size.width,40)
topStroke:[UIColor colorWithR:119 G:119 B:119 A:1].CGColor
lightColor:[UIColor colorWithR:92 G:92 B:92 A:1].CGColor
darkColor:[UIColor colorWithR:70 G:70 B:70 A:1].CGColor
bottomStroke:[UIColor colorWithR:76 G:76 B:76 A:1].CGColor
borderColor:[UIColor colorWithR:110 G:110 B:110 A:1].CGColor];
}
Note: I am not sure if this is the way to do it. I just want to initialise the background with a different gradient theme.
Anyways, it gives me an error. If I manually write in the drawRect an assignment of a [UIColor colorWith] method, that works. If I instead allow for code reuse, it crashes.
Any ideas?
Ben
One problem I see in your program is that you do not perform memory management of your
CGColorRefs — ARC (assuming you have it enabled) does not manage non-ObjC objects for you. You must retain and release these manually. So in both ARC and MRC, you must retain and release these manually.This is the basic form: