forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDIPickerViewController.m
137 lines (96 loc) · 3.43 KB
/
CDIPickerViewController.m
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
//
// CDIPickerViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 7/29/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDIPickerViewController.h"
#import "CDIGroupedTableViewCell.h"
@implementation CDIPickerViewController
#pragma mark - Accessors
@synthesize currentIndexPath = _currentIndexPath;
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *selectedKey = [[self class] selectedKey];
if (selectedKey != nil) {
self.currentIndexPath = [NSIndexPath indexPathForRow:[self.keys indexOfObject:selectedKey] inSection:0];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
}
#pragma mark - Picker
+ (NSString *)defaultsKey {
return nil;
}
+ (NSString *)selectedKey {
return [[NSUserDefaults standardUserDefaults] stringForKey:[self defaultsKey]];
}
+ (void)setSelectedKey:(NSString *)key {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:key forKey:[self defaultsKey]];
[userDefaults synchronize];
}
// This method may be overridden by a subclass
- (NSString *)cellTextForKey:(id)key {
return [[self class] textForKey:key];
}
// This method should be overridden by a subclass
- (UIImage *)cellImageForKey:(id)key {
return nil;
}
// This method must be overridden by a subclass
- (NSArray *)keys {
return nil;
}
// This method must be overridden by a subclass
+ (NSDictionary *)valueMap {
return nil;
}
+ (NSString *)textForKey:(NSString *)key {
return [[self valueMap] objectForKey:key];
}
+ (NSString *)textForSelectedKey {
return [self textForKey:[self selectedKey]];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (NSInteger)[self.keys count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"None";
NSUInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
if (numberOfRows == 1) {
cellIdentifier = @"Both";
} else if (indexPath.row == 0) {
cellIdentifier = @"Top";
} else if (indexPath.row == numberOfRows - 1) {
cellIdentifier = @"Bottom";
}
CDIGroupedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CDIGroupedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
id key = [self.keys objectAtIndex:indexPath.row];
cell.textLabel.text = [self cellTextForKey:key];
cell.imageView.image = [self cellImageForKey:key];
if([key isEqual:[[self class] selectedKey]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.currentIndexPath = indexPath;
[[self class] setSelectedKey:[self.keys objectAtIndex:indexPath.row]];
[self.navigationController popViewControllerAnimated:YES];
}
@end