This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
/
SSPickerViewController.m
executable file
·102 lines (72 loc) · 2.48 KB
/
SSPickerViewController.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
//
// SSPickerViewController.m
// SSToolkit
//
// Created by Sam Soffes on 10/9/08.
// Copyright 2008-2011 Sam Soffes. All rights reserved.
//
#import "SSPickerViewController.h"
@implementation SSPickerViewController
@synthesize selectedKey = _selectedKey;
@synthesize keys = _keys;
@synthesize currentIndexPath = _currentIndexPath;
#pragma mark NSObject
- (id)init {
self = [super initWithStyle:UITableViewStyleGrouped];
return self;
}
- (void)dealloc {
[_keys release];
[_selectedKey release];
[_currentIndexPath release];
[super dealloc];
}
#pragma mark UIViewController Methods
- (void)viewDidLoad {
[super viewDidLoad];
[self loadKeys];
if(self.selectedKey != nil) {
self.currentIndexPath = [[NSIndexPath indexPathForRow:[self.keys indexOfObject:self.selectedKey] inSection:0] retain];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
}
#pragma mark SSPickerViewController
// This method should be overridden by a subclass
- (void)loadKeys {
self.keys = nil;
self.selectedKey = nil;
}
// This method should be overridden by a subclass
- (NSString *)cellTextForKey:(NSString *)key {
return key;
}
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.currentIndexPath = indexPath;
}
#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 {
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
}
NSString *key = [self.keys objectAtIndex:indexPath.row];
cell.textLabel.text = [self cellTextForKey:key];
if([key isEqual:self.selectedKey] == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
@end