This repository was archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathTableViewController.m
More file actions
223 lines (171 loc) · 6.85 KB
/
TableViewController.m
File metadata and controls
223 lines (171 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// TableViewController.m
// Sample
//
// Created by Ignacio Romero Z. on 1/26/15.
// Copyright (c) 2015 DZN Labs. All rights reserved.
//
#import "TableViewController.h"
#import "DZNSegmentedControl.h"
#define DEBUG_APPERANCE 0
#define DEBUG_IMAGE 0
#define kBakgroundColor [UIColor colorWithRed:0/255.0 green:87/255.0 blue:173/255.0 alpha:1.0]
#define kTintColor [UIColor colorWithRed:20/255.0 green:200/255.0 blue:255/255.0 alpha:1.0]
#define kHairlineColor [UIColor colorWithRed:0/255.0 green:36/255.0 blue:100/255.0 alpha:1.0]
@interface TableViewController () <DZNSegmentedControlDelegate>
@property (nonatomic, strong) DZNSegmentedControl *control;
@property (nonatomic, strong) NSArray *menuItems;
@end
@implementation TableViewController
+ (void)initialize
{
#if DEBUG_APPERANCE
[[DZNSegmentedControl appearance] setBackgroundColor:kBakgroundColor];
[[DZNSegmentedControl appearance] setTintColor:kTintColor];
[[DZNSegmentedControl appearance] setHairlineColor:kHairlineColor];
[[DZNSegmentedControl appearance] setFont:[UIFont fontWithName:@"EuphemiaUCAS" size:15.0]];
[[DZNSegmentedControl appearance] setSelectionIndicatorHeight:2.5];
[[DZNSegmentedControl appearance] setAnimationDuration:0.125];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor], NSFontAttributeName: [UIFont systemFontOfSize:18.0]}];
#endif
}
- (void)loadView
{
[super loadView];
self.title = NSStringFromClass([DZNSegmentedControl class]);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addSegment:)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshSegments:)];
}
- (void)viewDidLoad
{
[super viewDidLoad];
#if DEBUG_IMAGE
_menuItems = @[[UIImage imageNamed:@"icn_clock"], [UIImage imageNamed:@"icn_emoji"], [UIImage imageNamed:@"icn_gift"]];
#else
_menuItems = @[[@"Tweets" uppercaseString], [@"Following" uppercaseString], [@"Followers" uppercaseString]];
#endif
self.tableView.tableHeaderView = self.control;
self.tableView.tableFooterView = [UIView new];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.clipsToBounds = YES;
[self updateControlCounts];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (DZNSegmentedControl *)control
{
if (!_control)
{
_control = [[DZNSegmentedControl alloc] initWithItems:self.menuItems];
_control.delegate = self;
_control.selectedSegmentIndex = 1;
_control.bouncySelectionIndicator = NO;
_control.disableSelectedSegment = NO;
_control.height = 60.0f;
// _control.height = 120.0f;
// _control.width = 300.0f;
// _control.showsGroupingSeparators = YES;
// _control.inverseTitles = YES;
// _control.backgroundColor = [UIColor lightGrayColor];
// _control.tintColor = [UIColor purpleColor];
// _control.hairlineColor = [UIColor purpleColor];
// _control.showsCount = NO;
// _control.autoAdjustSelectionIndicatorWidth = NO;
// _control.selectionIndicatorHeight = 4.0;
// _control.adjustsFontSizeToFitWidth = YES;
[_control addTarget:self action:@selector(didChangeSegment:) forControlEvents:UIControlEventValueChanged];
}
return _control;
}
#pragma mark - UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.textColor = [UIColor darkGrayColor];
}
#if DEBUG_IMAGE
cell.textLabel.text = [NSString stringWithFormat:@"cell #%d", (int)indexPath.row+1];
#else
cell.textLabel.text = [NSString stringWithFormat:@"%@ #%d", [[self.control titleForSegmentAtIndex:self.control.selectedSegmentIndex] capitalizedString], (int)indexPath.row+1];
#endif
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark - ViewController Methods
- (void)addSegment:(id)sender
{
NSUInteger newSegment = self.control.numberOfSegments;
#if DEBUG_IMAGE
[self.control setImage:[UIImage imageNamed:@"icn_clock"] forSegmentAtIndex:newSegment];
#else
[self.control setTitle:[@"Favorites" uppercaseString] forSegmentAtIndex:newSegment];
[self.control setCount:@((arc4random()%10000)) forSegmentAtIndex:newSegment];
#endif
}
- (void)refreshSegments:(id)sender
{
NSMutableArray *array = [NSMutableArray arrayWithArray:self.menuItems];
NSUInteger count = [array count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
NSUInteger nElements = count - i;
NSUInteger n = (arc4random() % nElements) + i;
[array exchangeObjectAtIndex:i withObjectAtIndex:n];
}
_menuItems = array;
[self.control setItems:self.menuItems];
[self updateControlCounts];
}
- (void)updateControlCounts
{
[self.control setCount:@((arc4random()%10000)) forSegmentAtIndex:0];
[self.control setCount:@((arc4random()%10000)) forSegmentAtIndex:1];
[self.control setCount:@((arc4random()%10000)) forSegmentAtIndex:2];
#if DEBUG_APPERANCE
[self.control setTitleColor:kHairlineColor forState:UIControlStateNormal];
#endif
}
- (void)didChangeSegment:(DZNSegmentedControl *)control
{
NSLog(@"%s",__FUNCTION__);
[self.tableView reloadData];
}
#pragma mark - DZNSegmentedControlDelegate Methods
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)view
{
return UIBarPositionAny;
}
- (UIBarPosition)positionForSelectionIndicator:(id<UIBarPositioning>)bar
{
return UIBarPositionAny;
}
@end