Skip to content

Commit 65b823b

Browse files
committed
Added support for grouped UITableViewCell
1 parent e86c6b6 commit 65b823b

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed

Classes/ios/FUICellBackgroundView.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// UACellBackgroundView.h
3+
// FlatUI
4+
//
5+
// Created by Maciej Swic on 2013-05-30.
6+
// Licensed under the MIT license.
7+
8+
typedef enum {
9+
FUICellBackgroundViewPositionSingle = 0,
10+
FUICellBackgroundViewPositionTop,
11+
FUICellBackgroundViewPositionBottom,
12+
UACellBackgroundViewPositionMiddle
13+
} FUICellBackgroundViewPosition;
14+
15+
@interface FUICellBackgroundView : UIView
16+
17+
@property (nonatomic) FUICellBackgroundViewPosition position;
18+
@property (nonatomic, strong) UIColor *backgroundColor;
19+
@property (nonatomic) CGFloat cornerRadius;
20+
@property (nonatomic, strong) UIColor* separatorColor;
21+
@property (nonatomic) CGFloat separatorHeight;
22+
23+
- (id)initWithFrame:(CGRect)frame andTableView:(UITableView*)tableView andIndexPath:(NSIndexPath*)indexPath;
24+
25+
@end

Classes/ios/FUICellBackgroundView.m

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//
2+
// UACellBackgroundView.m
3+
// FlatUI
4+
//
5+
// Created by Maciej Swic on 2013-05-30.
6+
// Licensed under the MIT license.
7+
8+
#import "FUICellBackgroundView.h"
9+
10+
@implementation FUICellBackgroundView
11+
12+
- (id)initWithFrame:(CGRect)frame {
13+
if ((self = [super initWithFrame:frame])) {
14+
self.cornerRadius = 10.f;
15+
self.separatorHeight = 1.f;
16+
}
17+
18+
return self;
19+
}
20+
21+
- (id)initWithFrame:(CGRect)frame andTableView:(UITableView*)tableView andIndexPath:(NSIndexPath*)indexPath {
22+
if ((self = [self initWithFrame:frame])) {
23+
if ([tableView numberOfRowsInSection:indexPath.section] == 1)
24+
self.position = FUICellBackgroundViewPositionSingle;
25+
else if (indexPath.row == 0)
26+
self.position = FUICellBackgroundViewPositionTop;
27+
else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1)
28+
self.position = FUICellBackgroundViewPositionBottom;
29+
else
30+
self.position = UACellBackgroundViewPositionMiddle;
31+
32+
self.separatorColor = tableView.separatorColor;
33+
}
34+
35+
return self;
36+
}
37+
38+
- (BOOL)isOpaque {
39+
return NO;
40+
}
41+
42+
-(void)drawRect:(CGRect)aRect {
43+
CGContextRef c = UIGraphicsGetCurrentContext();
44+
45+
int lineWidth = 1;
46+
47+
CGRect rect = [self bounds];
48+
CGFloat minX = CGRectGetMinX(rect), midX = CGRectGetMidX(rect), maxX = CGRectGetMaxX(rect);
49+
CGFloat minY = CGRectGetMinY(rect), midY = CGRectGetMidY(rect), maxY = CGRectGetMaxY(rect);
50+
minY -= 1;
51+
52+
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
53+
CGContextSetStrokeColorWithColor(c, [[UIColor grayColor] CGColor]);
54+
CGContextSetLineWidth(c, lineWidth);
55+
CGContextSetAllowsAntialiasing(c, YES);
56+
CGContextSetShouldAntialias(c, YES);
57+
58+
if (self.position == FUICellBackgroundViewPositionTop) {
59+
minY += 1;
60+
61+
CGMutablePathRef path = CGPathCreateMutable();
62+
CGPathMoveToPoint(path, NULL, minX, maxY);
63+
CGPathAddArcToPoint(path, NULL, minX, minY, midX, minY, self.cornerRadius);
64+
CGPathAddArcToPoint(path, NULL, maxX, minY, maxX, maxY, self.cornerRadius);
65+
CGPathAddLineToPoint(path, NULL, maxX, maxY);
66+
CGPathAddLineToPoint(path, NULL, minX, maxY);
67+
CGPathCloseSubpath(path);
68+
69+
CGContextSaveGState(c);
70+
CGContextAddPath(c, path);
71+
CGContextClip(c);
72+
73+
CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor);
74+
CGContextFillRect(c, self.bounds);
75+
CGContextSetFillColorWithColor(c, self.separatorColor.CGColor);
76+
CGContextFillRect(c, CGRectMake(0, self.bounds.size.height - self.separatorHeight, self.bounds.size.width, self.bounds.size.height - self.separatorHeight));
77+
78+
CGContextAddPath(c, path);
79+
CGPathRelease(path);
80+
CGContextRestoreGState(c);
81+
} else if (self.position == FUICellBackgroundViewPositionBottom) {
82+
CGMutablePathRef path = CGPathCreateMutable();
83+
CGPathMoveToPoint(path, NULL, minX, minY);
84+
CGPathAddArcToPoint(path, NULL, minX, maxY, midX, maxY, self.cornerRadius);
85+
CGPathAddArcToPoint(path, NULL, maxX, maxY, maxX, minY, self.cornerRadius);
86+
CGPathAddLineToPoint(path, NULL, maxX, minY);
87+
CGPathAddLineToPoint(path, NULL, minX, minY);
88+
CGPathCloseSubpath(path);
89+
90+
CGContextSaveGState(c);
91+
CGContextAddPath(c, path);
92+
CGContextClip(c);
93+
94+
CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor);
95+
CGContextFillRect(c, self.bounds);
96+
97+
CGContextAddPath(c, path);
98+
CGPathRelease(path);
99+
CGContextRestoreGState(c);
100+
} else if (self.position == UACellBackgroundViewPositionMiddle) {
101+
CGMutablePathRef path = CGPathCreateMutable();
102+
CGPathMoveToPoint(path, NULL, minX, minY);
103+
CGPathAddLineToPoint(path, NULL, maxX, minY);
104+
CGPathAddLineToPoint(path, NULL, maxX, maxY);
105+
CGPathAddLineToPoint(path, NULL, minX, maxY);
106+
CGPathAddLineToPoint(path, NULL, minX, minY);
107+
CGPathCloseSubpath(path);
108+
109+
CGContextSaveGState(c);
110+
CGContextAddPath(c, path);
111+
CGContextClip(c);
112+
113+
CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor);
114+
CGContextFillRect(c, self.bounds);
115+
CGContextSetFillColorWithColor(c, self.separatorColor.CGColor);
116+
CGContextFillRect(c, CGRectMake(0, self.bounds.size.height - self.separatorHeight, self.bounds.size.width, self.bounds.size.height - self.separatorHeight));
117+
118+
CGContextAddPath(c, path);
119+
CGPathRelease(path);
120+
CGContextRestoreGState(c);
121+
} else if (self.position == FUICellBackgroundViewPositionSingle) {
122+
minY += 1;
123+
124+
CGMutablePathRef path = CGPathCreateMutable();
125+
CGPathMoveToPoint(path, NULL, minX, midY);
126+
CGPathAddArcToPoint(path, NULL, minX, minY, midX, minY, self.cornerRadius);
127+
CGPathAddArcToPoint(path, NULL, maxX, minY, maxX, midY, self.cornerRadius);
128+
CGPathAddArcToPoint(path, NULL, maxX, maxY, midX, maxY, self.cornerRadius);
129+
CGPathAddArcToPoint(path, NULL, minX, maxY, minX, midY, self.cornerRadius);
130+
CGPathCloseSubpath(path);
131+
132+
CGContextSaveGState(c);
133+
CGContextAddPath(c, path);
134+
CGContextClip(c);
135+
136+
CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor);
137+
CGContextFillRect(c, self.bounds);
138+
139+
CGContextAddPath(c, path);
140+
CGPathRelease(path);
141+
CGContextRestoreGState(c);
142+
}
143+
144+
CGColorSpaceRelease(colorspace);
145+
}
146+
147+
- (void)setPosition:(FUICellBackgroundViewPosition)position {
148+
_position = position;
149+
150+
[self setNeedsDisplay];
151+
}
152+
153+
@end

README.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ As above, we provide a category on UINavigationBar to configure it flatly with a
139139
[self.navigationController.navigationBar configureFlatNavigationBarWithColor:[UIColor midnightBlueColor]];
140140
```
141141
142+
### Grouped TableView Cells
143+
You can modify the backgroundColor and selectedBackgroundColor of a grouped UITableViewCell without losing the rounded corners. The cell will copy the UITableView's separator color. The separator height is exposed as separatorHeight and the radius as cornerRadius.
144+
145+
```objective-c
146+
FUICellBackgroundView* backgroundView = [[FUICellBackgroundView alloc] initWithFrame:cell.backgroundView.bounds andTableView:self.tableView andIndexPath:indexPath];
147+
backgroundView.backgroundColor = [UIColor midnightBlueColor];
148+
cell.backgroundView = backgroundView;
149+
```
150+
151+
The same goes for the selectedBackgroundColor, just assign the view to cell.selectedBackgroundColor.
152+
142153
Colors
143154
-------
144155

0 commit comments

Comments
 (0)