Skip to content

Commit aa2b977

Browse files
committed
Added a category method for configuring flat UITableViewCells
1 parent 65b823b commit aa2b977

File tree

9 files changed

+215
-27
lines changed

9 files changed

+215
-27
lines changed

Classes/ios/FUICellBackgroundView.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,4 @@ typedef enum {
2020
@property (nonatomic, strong) UIColor* separatorColor;
2121
@property (nonatomic) CGFloat separatorHeight;
2222

23-
- (id)initWithFrame:(CGRect)frame andTableView:(UITableView*)tableView andIndexPath:(NSIndexPath*)indexPath;
24-
2523
@end

Classes/ios/FUICellBackgroundView.m

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,37 @@ - (id)initWithFrame:(CGRect)frame {
1313
if ((self = [super initWithFrame:frame])) {
1414
self.cornerRadius = 10.f;
1515
self.separatorHeight = 1.f;
16+
self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
1617
}
1718

1819
return self;
1920
}
2021

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-
3822
- (BOOL)isOpaque {
3923
return NO;
4024
}
4125

42-
-(void)drawRect:(CGRect)aRect {
43-
CGContextRef c = UIGraphicsGetCurrentContext();
26+
- (void)layoutSubviews {
27+
[super layoutSubviews];
28+
29+
//Determine position
30+
UITableView* tableView = (UITableView*)self.superview.superview;
31+
NSIndexPath* indexPath = [tableView indexPathForCell:(UITableViewCell*)self.superview];
32+
33+
if ([tableView numberOfRowsInSection:indexPath.section] == 1)
34+
self.position = FUICellBackgroundViewPositionSingle;
35+
else if (indexPath.row == 0)
36+
self.position = FUICellBackgroundViewPositionTop;
37+
else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1)
38+
self.position = FUICellBackgroundViewPositionBottom;
39+
else
40+
self.position = UACellBackgroundViewPositionMiddle;
41+
42+
self.separatorColor = tableView.separatorColor;
43+
}
44+
45+
- (void)drawRect:(CGRect)aRect {
46+
CGContextRef c = UIGraphicsGetCurrentContext();
4447

4548
int lineWidth = 1;
4649

Classes/ios/UITableViewCell+FlatUI.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// UITableViewCell+FlatUI.h
3+
// FlatUIKitExample
4+
//
5+
// Created by Maciej Swic on 2013-05-31.
6+
//
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UITableViewCell (FlatUI)
12+
13+
@property (nonatomic) CGFloat cornerRadius;
14+
@property (nonatomic) CGFloat separatorHeight;
15+
16+
+ (UITableViewCell*) configureFlatCellWithColor:(UIColor *)color selectedColor:(UIColor *)selectedColor style:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier;
17+
18+
- (void)setCornerRadius:(CGFloat)cornerRadius;
19+
- (void)setSeparatorHeight:(CGFloat)separatorHeight;
20+
21+
@end

Classes/ios/UITableViewCell+FlatUI.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// UITableViewCell+FlatUI.m
3+
// FlatUIKitExample
4+
//
5+
// Created by Maciej Swic on 2013-05-31.
6+
//
7+
//
8+
9+
#import "UITableViewCell+FlatUI.h"
10+
#import "FUICellBackgroundView.h"
11+
#import <objc/runtime.h>
12+
13+
@implementation UITableViewCell (FlatUI)
14+
15+
@dynamic cornerRadius, separatorHeight;
16+
17+
+ (UITableViewCell*) configureFlatCellWithColor:(UIColor *)color selectedColor:(UIColor *)selectedColor style:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {
18+
UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:reuseIdentifier];
19+
20+
FUICellBackgroundView* backgroundView = [FUICellBackgroundView new];
21+
backgroundView.backgroundColor = color;
22+
cell.backgroundView = backgroundView;
23+
24+
FUICellBackgroundView* selectedBackgroundView = [FUICellBackgroundView new];
25+
selectedBackgroundView.backgroundColor = selectedColor;
26+
cell.selectedBackgroundView = selectedBackgroundView;
27+
28+
//The labels need a clear background color or they will look very funky
29+
cell.textLabel.backgroundColor = [UIColor clearColor];
30+
if ([cell respondsToSelector:@selector(detailTextLabel)])
31+
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
32+
33+
//Guess some good text colors
34+
cell.textLabel.textColor = selectedColor;
35+
cell.textLabel.highlightedTextColor = color;
36+
if ([cell respondsToSelector:@selector(detailTextLabel)]) {
37+
cell.detailTextLabel.textColor = selectedColor;
38+
cell.detailTextLabel.highlightedTextColor = color;
39+
}
40+
41+
return cell;
42+
}
43+
44+
- (void)setCornerRadius:(CGFloat)cornerRadius {
45+
[(FUICellBackgroundView*)self.backgroundView setCornerRadius:cornerRadius];
46+
[(FUICellBackgroundView*)self.selectedBackgroundView setCornerRadius:cornerRadius];
47+
}
48+
49+
- (void)setSeparatorHeight:(CGFloat)separatorHeight {
50+
[(FUICellBackgroundView*)self.backgroundView setSeparatorHeight:separatorHeight];
51+
[(FUICellBackgroundView*)self.selectedBackgroundView setSeparatorHeight:separatorHeight];
52+
}
53+
54+
@end

Example/FlatUIKitExample.xcodeproj/project.pbxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/* Begin PBXBuildFile section */
1010
6F4F7CD41746884200D9F472 /* FUISegmentedControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 6F4F7CD31746884200D9F472 /* FUISegmentedControl.m */; };
1111
6FF9CE3C1745476B004BEFAD /* UIProgressView+FlatUI.m in Sources */ = {isa = PBXBuildFile; fileRef = 6FF9CE3B1745476B004BEFAD /* UIProgressView+FlatUI.m */; };
12+
C1DA68331758953900926483 /* UITableViewCell+FlatUI.m in Sources */ = {isa = PBXBuildFile; fileRef = C1DA68321758953900926483 /* UITableViewCell+FlatUI.m */; };
13+
C1DA6839175895C500926483 /* FUICellBackgroundView.m in Sources */ = {isa = PBXBuildFile; fileRef = C1DA6838175895C500926483 /* FUICellBackgroundView.m */; };
14+
C1DA683C1758972C00926483 /* TableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C1DA683B1758972C00926483 /* TableViewController.m */; };
1215
FA5311501744C7C1002711E1 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA53114F1744C7C1002711E1 /* UIKit.framework */; };
1316
FA5311521744C7C1002711E1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA5311511744C7C1002711E1 /* Foundation.framework */; };
1417
FA5311541744C7C1002711E1 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA5311531744C7C1002711E1 /* CoreGraphics.framework */; };
@@ -49,6 +52,12 @@
4952
6F4F7CD31746884200D9F472 /* FUISegmentedControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FUISegmentedControl.m; sourceTree = "<group>"; };
5053
6FF9CE3A1745476B004BEFAD /* UIProgressView+FlatUI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIProgressView+FlatUI.h"; sourceTree = "<group>"; };
5154
6FF9CE3B1745476B004BEFAD /* UIProgressView+FlatUI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIProgressView+FlatUI.m"; sourceTree = "<group>"; };
55+
C1DA68311758953900926483 /* UITableViewCell+FlatUI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITableViewCell+FlatUI.h"; sourceTree = "<group>"; };
56+
C1DA68321758953900926483 /* UITableViewCell+FlatUI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITableViewCell+FlatUI.m"; sourceTree = "<group>"; };
57+
C1DA6837175895C500926483 /* FUICellBackgroundView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FUICellBackgroundView.h; sourceTree = "<group>"; };
58+
C1DA6838175895C500926483 /* FUICellBackgroundView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FUICellBackgroundView.m; sourceTree = "<group>"; };
59+
C1DA683A1758972C00926483 /* TableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TableViewController.h; sourceTree = "<group>"; };
60+
C1DA683B1758972C00926483 /* TableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TableViewController.m; sourceTree = "<group>"; };
5261
FA53114C1744C7C1002711E1 /* FlatUIKitExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FlatUIKitExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
5362
FA53114F1744C7C1002711E1 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
5463
FA5311511744C7C1002711E1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -150,6 +159,8 @@
150159
children = (
151160
FA53115E1744C7C1002711E1 /* AppDelegate.h */,
152161
FA53115F1744C7C1002711E1 /* AppDelegate.m */,
162+
C1DA683A1758972C00926483 /* TableViewController.h */,
163+
C1DA683B1758972C00926483 /* TableViewController.m */,
153164
FA5311671744C7C1002711E1 /* ViewController.h */,
154165
FA6498981744CACB004B1A74 /* ViewController.m */,
155166
FA5311681744C7C1002711E1 /* ViewController.xib */,
@@ -206,6 +217,8 @@
206217
FA5311781744C920002711E1 /* FUIAlertView.m */,
207218
FA5311791744C920002711E1 /* FUIButton.h */,
208219
FA53117A1744C920002711E1 /* FUIButton.m */,
220+
C1DA6837175895C500926483 /* FUICellBackgroundView.h */,
221+
C1DA6838175895C500926483 /* FUICellBackgroundView.m */,
209222
FA53117B1744C920002711E1 /* FUISwitch.h */,
210223
FA53117C1744C920002711E1 /* FUISwitch.m */,
211224
FA53117D1744C920002711E1 /* UIBarButtonItem+FlatUI.h */,
@@ -226,6 +239,8 @@
226239
FA53118C1744C920002711E1 /* UITabBar+FlatUI.m */,
227240
6F4F7CD21746884200D9F472 /* FUISegmentedControl.h */,
228241
6F4F7CD31746884200D9F472 /* FUISegmentedControl.m */,
242+
C1DA68311758953900926483 /* UITableViewCell+FlatUI.h */,
243+
C1DA68321758953900926483 /* UITableViewCell+FlatUI.m */,
229244
);
230245
path = ios;
231246
sourceTree = "<group>";
@@ -339,6 +354,9 @@
339354
FA6498991744CACB004B1A74 /* ViewController.m in Sources */,
340355
6FF9CE3C1745476B004BEFAD /* UIProgressView+FlatUI.m in Sources */,
341356
6F4F7CD41746884200D9F472 /* FUISegmentedControl.m in Sources */,
357+
C1DA68331758953900926483 /* UITableViewCell+FlatUI.m in Sources */,
358+
C1DA6839175895C500926483 /* FUICellBackgroundView.m in Sources */,
359+
C1DA683C1758972C00926483 /* TableViewController.m in Sources */,
342360
);
343361
runOnlyForDeploymentPostprocessing = 0;
344362
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// TableViewController.h
3+
// FlatUIKitExample
4+
//
5+
// Created by Maciej Swic on 2013-05-31.
6+
//
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface TableViewController : UITableViewController
12+
13+
@end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// TableViewController.m
3+
// FlatUIKitExample
4+
//
5+
// Created by Maciej Swic on 2013-05-31.
6+
//
7+
//
8+
9+
#import "TableViewController.h"
10+
11+
#import "UITableViewCell+FlatUI.h"
12+
#import "UIColor+FlatUI.h"
13+
14+
@interface TableViewController ()
15+
16+
@end
17+
18+
@implementation TableViewController
19+
20+
- (id)initWithStyle:(UITableViewStyle)style
21+
{
22+
self = [super initWithStyle:style];
23+
if (self) {
24+
// Custom initialization
25+
}
26+
return self;
27+
}
28+
29+
- (void)viewDidLoad
30+
{
31+
[super viewDidLoad];
32+
33+
self.title = @"Grouped TableView";
34+
35+
//Set the separator color
36+
self.tableView.separatorColor = [UIColor cloudsColor];
37+
}
38+
39+
#pragma mark - Table view data source
40+
41+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
42+
{
43+
return 2;
44+
}
45+
46+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
47+
{
48+
if (section == 0)
49+
return 2;
50+
else
51+
return 1;
52+
}
53+
54+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
55+
{
56+
static NSString *CellIdentifier = @"Cell";
57+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
58+
59+
if (!cell) {
60+
cell = [UITableViewCell configureFlatCellWithColor:[UIColor midnightBlueColor] selectedColor:[UIColor cloudsColor] style:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
61+
cell.cornerRadius = 5.f; //Optional
62+
cell.separatorHeight = 2.f; //Optional
63+
}
64+
65+
cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
66+
67+
return cell;
68+
}
69+
70+
#pragma mark - Table view delegate
71+
72+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
73+
{
74+
[tableView deselectRowAtIndexPath:indexPath animated:YES];
75+
}
76+
77+
@end

Example/FlatUIKitExample/ViewController.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "ViewController.h"
10+
#import "TableViewController.h"
1011
#import "UIColor+FlatUI.h"
1112
#import "UISlider+FlatUI.h"
1213
#import "UIStepper+FlatUI.h"
@@ -51,10 +52,10 @@ - (void)viewDidLoad
5152
action:nil];
5253
[self.navigationItem.rightBarButtonItem removeTitleShadow];
5354

54-
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Button"
55+
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"TableView"
5556
style:UIBarButtonItemStylePlain
56-
target:nil
57-
action:nil];
57+
target:self
58+
action:@selector(showTableView:)];
5859
[self.navigationItem.rightBarButtonItem removeTitleShadow];
5960

6061
[self.navigationItem.leftBarButtonItem configureFlatButtonWithColor:[UIColor alizarinColor]
@@ -133,4 +134,9 @@ - (IBAction)pushViewController:(id)sender {
133134
[self.navigationController pushViewController:viewController animated:YES];
134135
}
135136

137+
- (void)showTableView:(id)sender {
138+
TableViewController* tableViewController = [[TableViewController alloc] initWithStyle:UITableViewStyleGrouped];
139+
[self.navigationController pushViewController:tableViewController animated:YES];
140+
}
141+
136142
@end

README.markdown

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ As above, we provide a category on UINavigationBar to configure it flatly with a
143143
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.
144144
145145
```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;
146+
cell = [UITableViewCell configureFlatCellWithColor:[UIColor midnightBlueColor] selectedColor:[UIColor cloudsColor] style:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
149147
```
150148

151149
The same goes for the selectedBackgroundColor, just assign the view to cell.selectedBackgroundColor.

0 commit comments

Comments
 (0)