I want to draw the background of a UITableViewCell which has a grouped style. The problem with me is I am not able to call the -(void)drawRect:(CGRect)rect
or I
If you are targeting iPhone OS 3.0+, all you need to do is this:
cell.backgroundColor = [UIColor redColor];
Substituting whatever UIColor you actually want, of course. The background color will be clipped automatically to the rounded rectangle bounds of the grouped table view cell.
HI GUYS,
I have got this code working....
just did this.....
In CustomCellBackGroundView
- (BOOL) isOpaque {
return NO;
}
- (id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setFillColor:[UIColor colorWithRed:0.07 green:.23 blue:.48 alpha:0.5]];
[self setBorderColor:[UIColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:0.5]];
[self setClearsContextBeforeDrawing:YES];
}
return self;
}
- (void)setNeedsDisplay{
[self setNeedsDisplayInRect:self.frame];
}
and in ur UITableViewController class just do this.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"My Identifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell==nil){
cell = [self reuseTableViewCellWithIdentifier:MyIdentifier indexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
CustomCellBackgroundView *custview = [[CustomCellBackgroundView alloc] initWithFrame:CGRectMake(0, 0, 320, 35)];
if(([tbleView numberOfRowsInSection:indexPath.section]-1) == 0){
custview.position = CustomCellBackgroundViewPositionTop;
}
else if(indexPath.row == 0){
custview.position = CustomCellBackgroundViewPositionTop;
}
else if (indexPath.row == ([tbleView numberOfRowsInSection:indexPath.section]-1)){
custview.position = CustomCellBackgroundViewPositionBottom;
}
else{
custview.position = CustomCellBackgroundViewPositionMiddle;
}
[cell setBackgroundView:custview];
[custview release];
UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:101];
[lbl setText:@"Hurray and thanks to all folks"];
//[cell setNeedsDisplay];
return cell;
}
remeber dont do this in (cell==nil) block and give special thanks to all folks at
How to customize the background/border colors of a grouped table view cell?
I have to agree with Sbrocket.
You need two lines of code. Four lines if you want to use a custom color for the line separator and border.
// For cell background
cell.backgroundColor=[UIColor lightGrayColor];
// For tableView line separator (assuming IBOutlet UITableView* table;)
self.table.separatorColor=[UIColor redColor];
//If you need a custom color (I assume you do), then...
cell.backgroundColor=[UIColor colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha];
Drop in the RGB value for any color you like for both the cell background and line separator.
Couldn't be easier.