3
3
#import " MaterialSwitch.h"
4
4
#import " MaterialTypography.h"
5
5
6
- static CGFloat kPestoSettingsViewControllerCellViewInset = 20 .f;
7
- static CGFloat kPestoSettingsViewControllerCellViewHeight = 48 .f;
6
+ static CGFloat kPestoSettingsTableViewOffsetTop = 10 .f;
8
7
9
- @interface PestoSettingsViewControllerCellView : UIView
8
+ static NSString *const kPestoSettingsTableViewCellReuseIdentifier = @" PestoSettingsTableViewCell" ;
9
+ static NSString *const kPestoSettingsTableViewHeaderViewReuseIdentifier =
10
+ @" PestoSettingsTableViewHeaderView" ;
11
+
12
+ static CGFloat kPestoSettingsTableViewHeaderSeparatorWidth = 1 .f;
13
+
14
+ @interface PestoSettingsTableViewCell : UITableViewCell
10
15
11
16
@property (nonatomic ) NSString *labelText;
12
17
@property (nonatomic ) BOOL on;
13
18
14
19
@end
15
20
16
- @interface PestoSettingsViewControllerCellView ()
21
+ @interface PestoSettingsTableViewCell ()
17
22
18
- @property (nonatomic ) UILabel *labelView;
19
23
@property (nonatomic ) MDCSwitch *switchView;
20
24
21
25
@end
22
26
23
- @implementation PestoSettingsViewControllerCellView
27
+ @implementation PestoSettingsTableViewCell
24
28
25
- - (id )initWithCoder : (NSCoder *)aDecoder {
26
- self = [super initWithCoder: aDecoder];
27
- return self;
28
- }
29
-
30
- - (id )initWithFrame : (CGRect)frame {
31
- self = [super initWithFrame: frame];
29
+ - (id )initWithStyle : (UITableViewCellStyle)style reuseIdentifier : (NSString *)reuseIdentifier {
30
+ self = [super initWithStyle: style reuseIdentifier: reuseIdentifier];
32
31
if (self) {
33
- CGRect descRect = CGRectMake (0 , 0 , self.bounds .size .width * 0 .75f , self.bounds .size .height );
34
- _labelView = [[UILabel alloc ] initWithFrame: descRect];
35
- _labelView.font = [MDCTypography body1Font ];
36
- _labelView.alpha = [MDCTypography body1FontOpacity ];
37
- _labelView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
38
- UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
32
+ self.textLabel .font = [MDCTypography body1Font ];
33
+ self.textLabel .alpha = [MDCTypography body1FontOpacity ];
39
34
40
35
_switchView = [[MDCSwitch alloc ] initWithFrame: CGRectZero];
41
- CGFloat switchX = self.bounds .size .width - _switchView.frame .size .width ;
42
- CGFloat switchY = (self.bounds .size .height - _switchView.frame .size .height ) / 2 .f ;
43
- _switchView.frame = CGRectOffset (_switchView.frame , switchX, switchY);
44
36
_switchView.onTintColor = [UIColor colorWithRed: 0 .09f green: 0 .54f blue: 0 .44f alpha: 1 .f];
45
- _switchView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
46
- UIViewAutoresizingFlexibleTopMargin |
47
- UIViewAutoresizingFlexibleBottomMargin;
48
-
49
- [self addSubview: _labelView];
50
- [self addSubview: _switchView];
37
+ self.accessoryView = _switchView;
51
38
}
52
39
return self;
53
40
}
54
41
55
42
- (void )setLabelText : (NSString *)labelText {
56
43
_labelText = labelText;
57
- _labelView.text = _labelText;
58
- [self setNeedsLayout ];
44
+ self.textLabel .text = _labelText;
59
45
}
60
46
61
47
- (void )setOn : (BOOL )on {
@@ -65,24 +51,152 @@ - (void)setOn:(BOOL)on {
65
51
66
52
@end
67
53
54
+ @interface PestoSettingsTableViewHeaderView : UITableViewHeaderFooterView
55
+
56
+ @property (nonatomic ) UIColor *separatorColor;
57
+
58
+ @end
59
+
60
+ @interface PestoSettingsTableViewHeaderView ()
61
+
62
+ @property (nonatomic ) CALayer *separator;
63
+
64
+ @end
65
+
66
+ @implementation PestoSettingsTableViewHeaderView
67
+
68
+ - (id )initWithReuseIdentifier : (NSString *)reuseIdentifier {
69
+ self = [super initWithReuseIdentifier: reuseIdentifier];
70
+ if (self) {
71
+ self.textLabel .font = [MDCTypography headlineFont ];
72
+ self.textLabel .alpha = [MDCTypography headlineFontOpacity ];
73
+ self.textLabel .textColor = [UIColor colorWithRed: 0 .09f green: 0 .54f blue: 0 .44f alpha: 1 .f];
74
+
75
+ self.backgroundView = [[UIView alloc ] initWithFrame: self .bounds];
76
+ self.backgroundView .backgroundColor = [UIColor whiteColor ];
77
+
78
+ _separator = [CALayer layer ];
79
+ [self .contentView.layer addSublayer: _separator];
80
+ }
81
+ return self;
82
+ }
83
+
84
+ - (void )layoutSubviews {
85
+ [super layoutSubviews ];
86
+ CGFloat borderBottomYPos = CGRectGetMaxY (self.contentView .bounds ) -
87
+ kPestoSettingsTableViewHeaderSeparatorWidth ;
88
+ _separator.frame = CGRectMake (0 ,
89
+ borderBottomYPos,
90
+ CGRectGetWidth (self.contentView .bounds ),
91
+ kPestoSettingsTableViewHeaderSeparatorWidth );
92
+ self.backgroundView .frame = self.bounds ;
93
+ }
94
+
95
+ - (void )setSeparatorColor : (UIColor *)separatorColor {
96
+ _separatorColor = separatorColor;
97
+ _separator.backgroundColor = _separatorColor.CGColor ;
98
+ }
99
+
100
+ @end
101
+
102
+ @interface PestoSettingsViewController () <UITableViewDataSource, UITableViewDelegate>
103
+
104
+ @property (nonatomic ) NSArray *dummySettingHeaders;
105
+ @property (nonatomic ) NSArray *dummySettingTitles;
106
+ @property (nonatomic ) NSArray *dummySettingVals;
107
+ @property (nonatomic ) UITableView *settingsTableView;
108
+
109
+ @end
110
+
68
111
@implementation PestoSettingsViewController
69
112
70
113
- (void )viewDidLoad {
71
114
[super viewDidLoad ];
72
- CGRect testViewFrame = CGRectMake (0 ,
73
- 10 .f ,
74
- self.view .bounds .size .width ,
75
- kPestoSettingsViewControllerCellViewHeight );
76
- CGRect insetFrame = CGRectInset (testViewFrame, kPestoSettingsViewControllerCellViewInset , 0 );
77
- PestoSettingsViewControllerCellView *testCellView = [[PestoSettingsViewControllerCellView alloc ]
78
- initWithFrame: insetFrame];
79
- testCellView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
80
- testCellView.labelText = @" Settings view needs more work?" ;
81
- testCellView.on = YES ;
82
115
83
116
self.view .backgroundColor = [UIColor whiteColor ];
84
117
85
- [self .view addSubview: testCellView];
118
+ _dummySettingHeaders = @[ @" Account" , @" Notification" ];
119
+ _dummySettingTitles = @[ @[ @" Public Profile" , @" Subscribe to Daily Digest" ],
120
+ @[ @" Get email notifications" , @" Get text notifications" ] ];
121
+ _dummySettingVals = @[ @[ @YES , @NO ], @[ @NO , @YES ] ];
122
+
123
+ CGRect settingsTableViewFrame =
124
+ CGRectMake (0 ,
125
+ kPestoSettingsTableViewOffsetTop ,
126
+ self.view .bounds .size .width ,
127
+ self.view .bounds .size .height - kPestoSettingsTableViewOffsetTop );
128
+ _settingsTableView = [[UITableView alloc ] initWithFrame: settingsTableViewFrame
129
+ style: UITableViewStylePlain];
130
+ _settingsTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
131
+ _settingsTableView.allowsSelection = NO ;
132
+ _settingsTableView.backgroundColor = self.view .backgroundColor ;
133
+ _settingsTableView.dataSource = self;
134
+ _settingsTableView.delegate = self;
135
+ _settingsTableView.separatorColor = [[self class ] tableViewSeparatorColor ];
136
+ // Ensure empty rows are not shown.
137
+ _settingsTableView.tableFooterView = [[UIView alloc ] initWithFrame: CGRectZero];
138
+
139
+ [_settingsTableView registerClass: [PestoSettingsTableViewCell class ]
140
+ forCellReuseIdentifier: kPestoSettingsTableViewCellReuseIdentifier ];
141
+ [_settingsTableView registerClass: [PestoSettingsTableViewHeaderView class ]
142
+ forHeaderFooterViewReuseIdentifier: kPestoSettingsTableViewHeaderViewReuseIdentifier ];
143
+
144
+ [_settingsTableView reloadData ];
145
+
146
+ [self .view addSubview: _settingsTableView];
147
+ }
148
+
149
+ + (UIColor *)tableViewSeparatorColor {
150
+ return [UIColor colorWithWhite: 0 .f alpha: 0 .1f ];
151
+ }
152
+
153
+ #pragma mark - UITableViewDataSource
154
+
155
+ - (NSInteger )numberOfSectionsInTableView : (UITableView *)tableView {
156
+ return [_dummySettingHeaders count ];
157
+ }
158
+
159
+ - (NSInteger )tableView : (UITableView *)tableView numberOfRowsInSection : (NSInteger )section {
160
+ return [(NSArray *)_dummySettingTitles[section] count ];
161
+ }
162
+
163
+ - (UITableViewCell *)tableView : (UITableView *)tableView
164
+ cellForRowAtIndexPath : (NSIndexPath *)indexPath {
165
+ NSString *settingLabel = _dummySettingTitles[indexPath.section][indexPath.row];
166
+ PestoSettingsTableViewCell *cell =
167
+ [tableView dequeueReusableCellWithIdentifier: kPestoSettingsTableViewCellReuseIdentifier
168
+ forIndexPath: indexPath];
169
+ cell.labelText = settingLabel;
170
+ cell.on = [_dummySettingVals[indexPath.section][indexPath.row] boolValue ];
171
+
172
+ return cell;
173
+ }
174
+
175
+ #pragma mark - UITableViewDelegate
176
+
177
+ - (UIView *)tableView : (UITableView *)tableView viewForHeaderInSection : (NSInteger )section {
178
+ NSString *reuseId = kPestoSettingsTableViewHeaderViewReuseIdentifier ;
179
+ PestoSettingsTableViewHeaderView *header =
180
+ [tableView dequeueReusableHeaderFooterViewWithIdentifier: reuseId];
181
+ header.textLabel .text = _dummySettingHeaders[section];
182
+ header.separatorColor = [[self class ] tableViewSeparatorColor ];
183
+ return header;
184
+ }
185
+
186
+ - (CGFloat)tableView : (UITableView *)tableView heightForHeaderInSection : (NSInteger )section {
187
+ return 50 .f ;
188
+ }
189
+
190
+ - (void )tableView : (UITableView *)tableView willDisplayCell : (nonnull UITableViewCell *)cell
191
+ forRowAtIndexPath : (nonnull NSIndexPath *)indexPath {
192
+ if ([cell respondsToSelector: @selector (setSeparatorInset: )]) {
193
+ [cell setSeparatorInset: UIEdgeInsetsZero];
194
+ }
195
+
196
+ if ([cell respondsToSelector: @selector (setLayoutMargins: )]) {
197
+ [cell setLayoutMargins: UIEdgeInsetsZero];
198
+ cell.preservesSuperviewLayoutMargins = NO ;
199
+ }
86
200
}
87
201
88
202
@end
0 commit comments