-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathSimpleTableViewSource.m
68 lines (57 loc) · 2.14 KB
/
SimpleTableViewSource.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
//
// Created by clowwindy on 6/6/13.
// Copyright (c) 2013 clowwindy. All rights reserved.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import "SimpleTableViewSource.h"
@implementation SimpleTableViewSource {
NSArray *_labels;
NSArray *_values;
NSObject *_value;
SimpleTableViewSourceSelectionBlock _selectionBlock;
}
- (id)initWithLabels:(NSArray *)labels values:(NSArray *)values initialValue:(NSObject *)value selectionBlock:(SimpleTableViewSourceSelectionBlock)block {
self = [super init];
if (self) {
_labels = labels;
_values = values;
_value = value;
_selectionBlock = block;
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = _labels[indexPath.row];
NSObject *currentValue = _values[indexPath.row];
if (currentValue == _value || [currentValue isEqual:_value]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _labels.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
_value = _values[newRow];
int rowCount = [tableView numberOfRowsInSection:0];
for (int i = 0; i<rowCount; i++) {
if (i != newRow) {
[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]].accessoryType = UITableViewCellAccessoryNone;
}
}
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
_selectionBlock(_value);
}
@end