Customize UITableViewCell background for grouped table view using UIBezierPath
When using UITableView
in iOS apps quite often you’d like to customize its design. In particular it is typical to change background color of cells and color of separators. It is usually trivial enough with plain style table views, however may get tricky when you have grouped style table view.
The problem is that if you change backgroundColor
property of a cell in a grouped table view you don’t get expected result. So the solution is to change backgroundView
of a cell instead. Quite common technique is to use images and so UIImageView
for the purpose. However if you want to keep standard look of cells but just change background color and border color it isn’t convenient.
So I’ve createad a reusable background view for this purpose. Thanks to UIBeizierPath
class its implementation is trivial, basically main code is drawRect:
method:
- (void)drawRect:(CGRect)rect
{
CGRect bounds = self.bounds;
UIBezierPath *path;
if (position == CellPositionSingle) {
path = [UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:kCornerRadius];
} else if (position == CellPositionTop) {
bounds.size.height += 1;
path = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(kCornerRadius, kCornerRadius)];
} else if (position == CellPositionBottom) {
path = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
cornerRadii:CGSizeMake(kCornerRadius, kCornerRadius)];
} else {
bounds.size.height += 1;
path = [UIBezierPath bezierPathWithRect:bounds];
}
[self.fillColor setFill];
[self.borderColor setStroke];
[path fill];
[path stroke];
}
Using the code is simple, it goes like this:
- (void)drawRect:(CGRect)rect
{
CGRect bounds = CGRectInset(self.bounds,
0.5 / [UIScreen mainScreen].scale,
0.5 / [UIScreen mainScreen].scale);
UIBezierPath *path;
if (position == CellPositionSingle) {
path = [UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:kCornerRadius];
} else if (position == CellPositionTop) {
bounds.size.height += 1;
path = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(kCornerRadius, kCornerRadius)];
} else if (position == CellPositionBottom) {
path = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
cornerRadii:CGSizeMake(kCornerRadius, kCornerRadius)];
} else {
bounds.size.height += 1;
path = [UIBezierPath bezierPathWithRect:bounds];
}
[self.fillColor setFill];
[self.borderColor setStroke];
[path fill];
[path stroke];
}
As a result you’ll get something like here:
Note that by using [UIColor colorWithPatternImage:]
you can use not only solid colors but textures or gradients as cell background.
Full code is available as Gist, feel free to fork it.
Shameless plug
We develop mobile and web apps, you can hire us to work on your next project.