-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTableView.m
More file actions
162 lines (126 loc) · 4.99 KB
/
SimpleTableView.m
File metadata and controls
162 lines (126 loc) · 4.99 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
//
// SimpleTableView.m
// WinePicker
//
// Created by Oleksii Nezhyborets on 21.08.15.
// Copyright (c) 2015 Onix-Systems. All rights reserved.
//
#import "SimpleTableView.h"
@interface SimpleTableViewCell : UITableViewCell
@property (nonatomic, weak) UIImageView *selectionImageView;
@property (nonatomic) CGSize selectionImageSize;
@end
@implementation SimpleTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIView *v = self.contentView;
UIImageView *imageView = [[UIImageView alloc] init];
[v addSubview:imageView];
self.selectionImageView = imageView;
_selectionImageSize = CGSizeZero;
[self remakeSelectionImageViewConstraints];
}
return self;
}
- (void)setSelectionImageSize:(CGSize)selectionImageSize {
if (!CGSizeEqualToSize(_selectionImageSize, selectionImageSize)) {
_selectionImageSize = selectionImageSize;
[self remakeSelectionImageViewConstraints];
}
}
- (void)remakeSelectionImageViewConstraints {
[self.selectionImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.trailing.equalTo(self.contentView.mas_trailingMargin);
make.centerY.equalTo(self.contentView);
if (!CGSizeEqualToSize(self.selectionImageSize, CGSizeZero)) {
make.width.equalTo(@(self.selectionImageSize.width));
make.height.equalTo(@(self.selectionImageSize.height));
}
}];
}
@end
@interface SimpleTableView() <UITableViewDataSource, UITableViewDelegate>
@end
@implementation SimpleTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
self = [super initWithFrame:frame style:style];
if (self) {
self.delegate = self;
self.dataSource = self;
self.removeMargins = YES;
}
return self;
}
- (void)setContentSize:(CGSize)contentSize {
[super setContentSize:contentSize];
if (self.shouldFitToContentSize) {
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame;
}
}
- (void)setItems:(NSArray *)items {
if (_items != items) {
_items = items;
[self reloadData];
}
}
- (void)hideSeparatorsForEmptyCells {
self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 0)];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"simpleTableIdentifier";
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[SimpleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
if (self.removeMargins) {
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
cell.indentationWidth = self.textX;
cell.indentationLevel = 1;
}
cell.selectionStyle = self.selectionStyle;
cell.selectionImageSize = self.imageSize;
}
NSString *value = self.items[indexPath.row];
cell.textLabel.text = value;
cell.textLabel.font = self.textFont;
cell.textLabel.textColor = self.textColor;
if ([value isEqualToString:self.selectedValue]) {
cell.selectionImageView.image = self.selectionImage;
} else {
cell.selectionImageView.image = nil;
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.items.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSMutableArray *indexPathsToReload = [[NSMutableArray alloc] init];
NSInteger previouslySelectedIndex = [self.items indexOfObject:self.selectedValue];
NSIndexPath *previouslySelectedIndexPath = [NSIndexPath indexPathForRow:previouslySelectedIndex inSection:0];
if (indexPath.row == previouslySelectedIndex) {
[indexPathsToReload addObject:previouslySelectedIndexPath];
self.selectedValue = nil;
if (self.didSelectBlock) {
self.didSelectBlock(nil);
}
} else {
if (previouslySelectedIndex == NSNotFound) {
[indexPathsToReload addObject:indexPath];
} else {
[indexPathsToReload addObject:previouslySelectedIndexPath];
[indexPathsToReload addObject:indexPath];
}
self.selectedValue = self.items[indexPath.row];
if (self.didSelectBlock) {
self.didSelectBlock(indexPath);
}
}
[tableView reloadRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationNone];
}
@end