-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathColorListMasterController.m
More file actions
194 lines (149 loc) · 6.12 KB
/
ColorListMasterController.m
File metadata and controls
194 lines (149 loc) · 6.12 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
//
// ConcreteMasterColorList.m
// APSplitSample
//
// Created by Slava on 5/5/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ColorListMasterController.h"
@interface ColorListMasterController()
- (UIColor *) randomColor;
- (UIViewController*)randomViewController;
- (void) buttonPushRandomViewController;
@end
@implementation ColorListMasterController
@synthesize colorList = _colorList;
@synthesize detailViewController = _detailViewController;
- (id)initWithSplit:(APSplitViewController*)split
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
_split = split;
// init detail
_detailViewController = [self randomViewController];
_detailViewController.title = @"Detail root";
[_split pushToDetailController:self.detailViewController];
// init list of color with root detail's color
_colorList = [[NSMutableArray alloc] initWithObjects:self.detailViewController.view.backgroundColor, nil];
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Push"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(buttonPushRandomViewController)];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
_detailViewController = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.colorList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
UIColor *color = [self.colorList objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"Colored Detail #%d", indexPath.row];
cell.textLabel.backgroundColor = color;
cell.contentView.backgroundColor = color;
return cell;
}
#pragma mark - Table view delegate
- (void)popToRowNumber:(int)rowNumber {
// update color list
int numberOfColorsToRemove = _colorList.count -rowNumber -1;
[_colorList removeObjectsInRange:NSMakeRange(rowNumber+1, numberOfColorsToRemove)];
// animate inserting to tableView
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:numberOfColorsToRemove];
for (int i=0; i<numberOfColorsToRemove; i++) {
NSIndexPath *index = [NSIndexPath indexPathForRow:_colorList.count+i inSection:0];
[indexPaths addObject:index];
}
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// update details view
UIViewController *controller = [_split.detail.viewControllers objectAtIndex:indexPath.row];
[_split.detail popToViewController:controller animated:YES];
[self popToRowNumber:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
#pragma mark - ColorListMasterDelegate events
- (void) colorViewDetailPoped {
if (_colorList.count > 1)
[self popToRowNumber:_colorList.count-2];
}
#pragma mark - Helpers
- (UIColor *)randomColor {
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
- (UIViewController*)randomViewController {
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [self randomColor];
return viewController;
}
- (void) buttonPushRandomViewController {
// get and push random controller
UIViewController *randomViewController = [self randomViewController];
randomViewController.title = [NSString stringWithFormat:@"Detail #%d", [_colorList count]];
[_split.detail pushViewController:randomViewController animated:YES];
// update color list
[_colorList addObject:randomViewController.view.backgroundColor];
// animate inserting to tableView
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_colorList.count-1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationTop];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
@end