-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMultiSelectViewController.m
More file actions
421 lines (347 loc) · 15.1 KB
/
Copy pathMultiSelectViewController.m
File metadata and controls
421 lines (347 loc) · 15.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// MultiSelectViewController.m
// MultiSelectTableViewController
//
// Created by molon on 6/7/14.
// Copyright (c) 2014 molon. All rights reserved.
//
#import "MultiSelectViewController.h"
#import "MLLetterIndexNavigationView.h"
#import "MultiSelectItem.h"
#import "MultiSelectTableViewCell.h"
#import "UIView+Convenience.h"
#import "MultiSelectedPanel.h"
#import "MultiSelectSearchResultTableViewCell.h"
#define kDefaultSectionHeaderHeight 22.0f
#define kDefaultRowHeight 55.0f
#define IOS_VERSION ([[[UIDevice currentDevice] systemVersion]floatValue])
@interface MultiSelectViewController ()<UITableViewDataSource,UITableViewDelegate,MLLetterIndexNavigationViewDelegate,MultiSelectedPanelDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *keys;
@property (nonatomic, strong) NSMutableDictionary *dict;
@property (nonatomic, strong) MLLetterIndexNavigationView *letterIndexView;
@property (nonatomic, strong) MultiSelectedPanel *selectedPanel;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) NSMutableArray *selectedItems;
@property (nonatomic, strong) NSMutableArray *selectedIndexes; //记录选择项对应的路径
@property (nonatomic, strong) NSArray *searchResult;
@end
@implementation MultiSelectViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dismiss
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"选择联系人";
self.view.backgroundColor = [UIColor colorWithWhite:1.000 alpha:1.000];
self.tableView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:1.000];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
//处理传递进来的数组成字典
self.dict = [NSMutableDictionary dictionary];
NSMutableArray *letters = [@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#"]mutableCopy];
for (NSString *letter in letters) {
self.dict[letter] = [NSMutableArray array];//先初始化其
}
for (MultiSelectItem *item in self.items) {
NSString *firstLetter = [self firstLetterOfString:item.name];
[self.dict[firstLetter] addObject:item]; //对应的字母数组添加元素。
}
//删除没有元素的字母key
for (NSString *key in [self.dict allKeys]) {
if (((NSArray*)self.dict[key]).count<=0) {
[self.dict removeObjectForKey:key];
[letters removeObject:key]; //由于字典是无序的,这里刚好把此数组作为有效key的排序结果。
}else{
//对这个数组进行字母排序,系统方法就可以对汉字排序,第一个汉字的首字母,第二个字母。。。这样来排序。
self.dict[key] = [((NSArray*)self.dict[key]) sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [((MultiSelectItem *)obj1).name localizedCompare:((MultiSelectItem *)obj2).name];
}];
}
}
//整理完毕,将key的排序结果记录
self.keys = letters;
self.letterIndexView.keys = self.keys;
//找到已经选择的项目
NSMutableArray *selectedItems = [NSMutableArray array];
NSMutableArray *selectedIndexes = [NSMutableArray array];
for (NSUInteger section=0; section<self.keys.count; section++) {
for (NSUInteger row=0; row<((NSArray*)self.dict[self.keys[section]]).count; row++) {
MultiSelectItem *item = self.dict[self.keys[section]][row];
if (!item.disabled&&item.selected) {
[selectedItems addObject:item];
[selectedIndexes addObject:[NSIndexPath indexPathForRow:row inSection:section]];
}
}
}
self.selectedItems = selectedItems;
self.selectedIndexes = selectedIndexes;
self.selectedPanel.selectedItems = self.selectedItems;
[self.view addSubview:self.tableView];
[self.view addSubview:self.letterIndexView];
[self.view addSubview:self.selectedPanel];
self.tableView.tableHeaderView = self.searchBar;
self.searchController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - getter
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]init];
_tableView.showsVerticalScrollIndicator = NO;
_tableView.scrollsToTop = YES;
_tableView.dataSource = self;
_tableView.delegate = self;
}
return _tableView;
}
- (MLLetterIndexNavigationView *)letterIndexView
{
if (!_letterIndexView) {
_letterIndexView = [[MLLetterIndexNavigationView alloc]init];
_letterIndexView.isNeedSearchIcon = YES;
_letterIndexView.delegate = self;
}
return _letterIndexView;
}
- (MultiSelectedPanel *)selectedPanel
{
if (!_selectedPanel) {
_selectedPanel = [MultiSelectedPanel instanceFromNib];
_selectedPanel.delegate = self;
}
return _selectedPanel;
}
- (UISearchBar *)searchBar
{
if (!_searchBar) {
UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frameWidth, 44.0f)];
searchBar.placeholder = @"搜索";
searchBar.delegate = self;
[searchBar sizeToFit];
_searchBar = searchBar;
}
return _searchBar;
}
- (UISearchDisplayController *)searchController
{
if (!_searchController) {
_searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
}
return _searchController;
}
#pragma mark - layout
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
#define kSelectPanelHeight 44.0f
self.tableView.frame = CGRectMake(0, 0, self.view.frameWidth, self.view.frameHeight-kSelectPanelHeight);
CGFloat topY = 0.0f;
//导航View的位置
if (IOS_VERSION>=7.0) { //简单处理下适配吧
topY += 20.0f;
topY += (self.view.frameHeight>self.view.frameWidth)?44.0f:32.0f;
}
self.letterIndexView.frame = CGRectMake(self.tableView.frameRight-20.0f, topY, 20.0f, self.view.frameHeight-kSelectPanelHeight-topY);
self.selectedPanel.frame = CGRectMake(0, self.tableView.frameBottom, self.view.frameWidth, kSelectPanelHeight);
}
#pragma mark - letter index delegate
- (void)mlLetterIndexNavigationView:(MLLetterIndexNavigationView *)mlLetterIndexNavigationView didSelectIndex:(NSInteger)index
{
if (index==0) {
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}else{
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index-1] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
}
#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([tableView isEqual:self.tableView]) {
return self.keys.count;
}
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([tableView isEqual:self.tableView]) {
return [((NSArray*)self.dict[self.keys[section]]) count];
}
return self.searchResult.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.tableView]) {
static NSString *CellIdentifier = @"MultiSelectTableViewCell";
MultiSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [MultiSelectTableViewCell instanceFromNib];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
MultiSelectItem *item = ((NSArray*)self.dict[self.keys[indexPath.section]])[indexPath.row];
[cell.cellImageView setImageWithURL:item.imageURL];
cell.label.text = item.name;
if (item.disabled) {
cell.selectState = MultiSelectTableViewSelectStateDisabled;
}else{
cell.selectState = item.selected?MultiSelectTableViewSelectStateSelected:MultiSelectTableViewSelectStateNoSelected;
}
return cell;
}
static NSString *CellIdentifier = @"MultiSelectSearchResultTableViewCell";
MultiSelectSearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [MultiSelectSearchResultTableViewCell instanceFromNib];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
MultiSelectItem *item = self.searchResult[indexPath.row];
[cell.cellImageView setImageWithURL:item.imageURL];
cell.contentLabel.text = item.name;
if (item.disabled) {
cell.contentLabel.textColor = [UIColor grayColor];
}
if (item.selected||item.disabled) {
cell.addedTipsLabel.hidden = NO;
}
return cell;
}
#pragma mark - tableView delegate
//section 头部,为了IOS6的美化
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if ([tableView isEqual:self.tableView]) {
UIView *customHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frameWidth, kDefaultSectionHeaderHeight)];
customHeaderView.backgroundColor = [UIColor colorWithRed:0.926 green:0.920 blue:0.956 alpha:1.000];
UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(15.0f, 0, customHeaderView.frameWidth-15.0f, kDefaultSectionHeaderHeight)];
headerLabel.text = self.keys[section];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:14.0f];
headerLabel.textColor = [UIColor darkGrayColor];
[customHeaderView addSubview:headerLabel];
return customHeaderView;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([tableView isEqual:self.tableView]) {
return kDefaultSectionHeaderHeight;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kDefaultRowHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([tableView isEqual:self.tableView]) {
MultiSelectItem *item = ((NSArray*)self.dict[self.keys[indexPath.section]])[indexPath.row];
if (item.disabled) {
return;
}
item.selected = !item.selected;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
if (item.selected) {
//告诉panel应该添加
[self.selectedItems addObject:item];
[self.selectedIndexes addObject:indexPath];
[self.selectedPanel didAddSelectedIndex:self.selectedItems.count-1];
}else{
//告诉panel应该删除
NSUInteger index = [self.selectedItems indexOfObject:item];
[self.selectedItems removeObject:item];
[self.selectedIndexes removeObject:indexPath];
if (index!=NSNotFound) {
[self.selectedPanel didDeleteSelectedIndex:index];
}
}
return;
}
//找到点击的item是在哪个
MultiSelectItem *item = self.searchResult[indexPath.row];
//找到位置
for (NSUInteger section=0; section<self.keys.count; section++) {
for (NSUInteger row=0; row<((NSArray*)self.dict[self.keys[section]]).count; row++) {
MultiSelectItem *aItem = self.dict[self.keys[section]][row];
if ([item isEqual:aItem]){
[self.searchController setActive:NO animated:YES];
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
//点击这个位置
if (!item.selected&&!item.disabled) {
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
//滚动到这个位置
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
break;
}
}
}
}
#pragma mark - common
- (NSString*)firstLetterOfString:(NSString*)chinese
{
CFMutableStringRef string = CFStringCreateMutableCopy(NULL, 0, (__bridge CFMutableStringRef)[NSMutableString stringWithString:chinese]);
CFStringTransform(string, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform(string, NULL, kCFStringTransformStripDiacritics, NO);
NSMutableString *aNSString = (__bridge NSMutableString *)string;
NSString *finalString = [aNSString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%c", 32] withString:@""];
NSString *firstLetter = [[finalString substringToIndex:1]uppercaseString];
if (!firstLetter||firstLetter.length<=0) {
firstLetter = @"#";
}else{
unichar letter = [firstLetter characterAtIndex:0];
if (letter<65||letter>90) {
firstLetter = @"#";
}
}
return firstLetter;
}
#pragma mark - selelcted panel delegate
- (void)willDeleteRowWithItem:(MultiSelectItem*)item withMultiSelectedPanel:(MultiSelectedPanel*)multiSelectedPanel
{
//在此做对数组元素的删除工作
NSUInteger index = [self.selectedItems indexOfObject:item];
if (index==NSNotFound) {
return;
}
item.selected = NO;
NSIndexPath *indexPath = self.selectedIndexes[index];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.selectedItems removeObjectAtIndex:index];
[self.selectedIndexes removeObjectAtIndex:index];
}
- (void)didConfirmWithMultiSelectedPanel:(MultiSelectedPanel*)multiSelectedPanel
{
NSLog(@"确认");
[self dismiss];
#warning 这里咋弄自己研究,已经选择的为self.selectedItems或者(原本传递进来的items里找selected=YES的)
}
#pragma mark - searchbar delegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//找到名字里有searchText关键字的
searchText = [searchText stringByReplacingOccurrencesOfString:@" " withString:@""];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchText];
self.searchResult = [self.items filteredArrayUsingPredicate:pre];
[self.searchController.searchResultsTableView reloadData];
}
@end