aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/iOSSampleApp/Shared/SkOptionListController.mm
blob: d5241348b1024533e894f80380513462461aa5a7 (plain)
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
#import "SkOptionListController.h"

@implementation SkOptionListController

@synthesize fOptions, fSelectedIndex, fSelectedCell, fParentCell;

#pragma mark -
#pragma mark Initialization

- (id)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:style];
    if (self) {
        self.fOptions = [[NSMutableArray alloc] init];
        self.fSelectedIndex = 0;
        self.fSelectedCell = nil;
    }
    return self;
}

- (void)addOption:(NSString*)option {
    [fOptions addObject:option];
}

- (NSString*)getSelectedOption {
    return (NSString*)[fOptions objectAtIndex:self.fSelectedIndex];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [fOptions 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] autorelease];
    }
    
    cell.textLabel.text = [fOptions objectAtIndex:indexPath.row];
    if (indexPath.row == fSelectedIndex) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.fSelectedCell = cell;
    }
    else
        cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    self.fSelectedCell.accessoryType = UITableViewCellAccessoryNone;
    self.fSelectedCell = cell;
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.fParentCell.detailTextLabel.text = cell.textLabel.text;;
    self.fSelectedIndex = indexPath.row;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
    self.fOptions = nil;
    self.fSelectedCell = nil;
    [super dealloc];
}

@end