|
| 1 | +// |
| 2 | +// CountryCodeListViewController.m |
| 3 | +// CodingMart |
| 4 | +// |
| 5 | +// Created by Ease on 16/5/11. |
| 6 | +// Copyright © 2016年 net.coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "CountryCodeListViewController.h" |
| 10 | +#import "CountryCodeCell.h" |
| 11 | + |
| 12 | +@interface CountryCodeListViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate> |
| 13 | +@property (strong, nonatomic) UISearchBar *mySearchBar; |
| 14 | +@property (strong, nonatomic) UITableView *myTableView; |
| 15 | +@property (strong, nonatomic) NSDictionary *countryCodeListDict, *searchResults; |
| 16 | +@property (strong, nonatomic) NSMutableArray *keyList; |
| 17 | + |
| 18 | +@end |
| 19 | + |
| 20 | +@implementation CountryCodeListViewController |
| 21 | +- (void)viewDidLoad{ |
| 22 | + [super viewDidLoad]; |
| 23 | + self.title = @"选择国家或地区"; |
| 24 | + _myTableView = ({ |
| 25 | + UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; |
| 26 | + tableView.backgroundColor = kColorTableSectionBg; |
| 27 | + tableView.delegate = self; |
| 28 | + tableView.dataSource = self; |
| 29 | + [tableView registerClass:[CountryCodeCell class] forCellReuseIdentifier:kCellIdentifier_CountryCodeCell]; |
| 30 | + tableView.sectionIndexBackgroundColor = [UIColor clearColor]; |
| 31 | + tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor]; |
| 32 | + tableView.sectionIndexColor = [UIColor colorWithHexString:@"0x666666"]; |
| 33 | + [self.view addSubview:tableView]; |
| 34 | + [tableView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 35 | + make.edges.equalTo(self.view); |
| 36 | + }]; |
| 37 | + tableView; |
| 38 | + }); |
| 39 | + _mySearchBar = ({ |
| 40 | + UISearchBar *searchBar = [UISearchBar new]; |
| 41 | + searchBar.delegate = self; |
| 42 | + [searchBar sizeToFit]; |
| 43 | + [searchBar setPlaceholder:@"国家/地区名称"]; |
| 44 | + searchBar; |
| 45 | + }); |
| 46 | + [self setupData]; |
| 47 | + |
| 48 | + _myTableView.tableHeaderView = _mySearchBar; |
| 49 | + _myTableView.tableFooterView = [UIView new]; |
| 50 | +} |
| 51 | + |
| 52 | +#pragma mark Data |
| 53 | +- (void)setupData{ |
| 54 | + NSString *path = [[NSBundle mainBundle] pathForResource:@"country_code" ofType:@"plist"]; |
| 55 | + _searchResults = _countryCodeListDict = [NSDictionary dictionaryWithContentsOfFile:path]; |
| 56 | + [self p_updateKeyList]; |
| 57 | +} |
| 58 | + |
| 59 | +- (void)p_updateKeyList{ |
| 60 | + _keyList = [_searchResults allKeys].mutableCopy; |
| 61 | + [_keyList sortUsingComparator:^NSComparisonResult(NSString * _Nonnull obj1, NSString * _Nonnull obj2) { |
| 62 | + if ([obj1 isEqualToString:@"#"]) { |
| 63 | + return NSOrderedAscending; |
| 64 | + }else if ([obj2 isEqualToString:@"#"]){ |
| 65 | + return NSOrderedDescending; |
| 66 | + }else{ |
| 67 | + return [obj1 compare:obj2]; |
| 68 | + } |
| 69 | + }]; |
| 70 | + [_keyList insertObject:UITableViewIndexSearch atIndex:0]; |
| 71 | +} |
| 72 | + |
| 73 | +#pragma mark Table M |
| 74 | + |
| 75 | +- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ |
| 76 | + if (scrollView == self.myTableView) { |
| 77 | + [_mySearchBar resignFirstResponder]; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ |
| 82 | + return _keyList.count - 1; |
| 83 | +} |
| 84 | + |
| 85 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 86 | + return [_searchResults[_keyList[section+ 1]] count]; |
| 87 | +} |
| 88 | + |
| 89 | +- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
| 90 | + return index > 0? index - 1: index; |
| 91 | +} |
| 92 | + |
| 93 | +-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ |
| 94 | + return _keyList; |
| 95 | +} |
| 96 | + |
| 97 | +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ |
| 98 | + return 20; |
| 99 | +} |
| 100 | + |
| 101 | +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ |
| 102 | + UIView *headerV = [UIView new]; |
| 103 | + headerV.backgroundColor = self.myTableView.backgroundColor; |
| 104 | + UILabel *titleL = [UILabel new]; |
| 105 | + titleL.font = [UIFont systemFontOfSize:12]; |
| 106 | + titleL.textColor = [UIColor colorWithHexString:@"0x999999"]; |
| 107 | + titleL.text = [_keyList[section+ 1] isEqualToString:@"#"]? @"常用地区": _keyList[section+ 1]; |
| 108 | + [headerV addSubview:titleL]; |
| 109 | + [titleL mas_makeConstraints:^(MASConstraintMaker *make) { |
| 110 | + make.edges.equalTo(headerV).insets(UIEdgeInsetsMake(4, 15, 4, 15)); |
| 111 | + }]; |
| 112 | + return headerV; |
| 113 | +} |
| 114 | + |
| 115 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 116 | + CountryCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_CountryCodeCell forIndexPath:indexPath]; |
| 117 | + cell.countryCodeDict = _searchResults[_keyList[indexPath.section+ 1]][indexPath.row]; |
| 118 | + return cell; |
| 119 | +} |
| 120 | + |
| 121 | +- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 122 | + cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, (kScreen_Width - cell.contentView.width) + 15); |
| 123 | +} |
| 124 | + |
| 125 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 126 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 127 | + if (_selectedBlock) { |
| 128 | + _selectedBlock(_searchResults[_keyList[indexPath.section+ 1]][indexPath.row]); |
| 129 | + } |
| 130 | + [self.navigationController popViewControllerAnimated:YES]; |
| 131 | +} |
| 132 | + |
| 133 | +#pragma mark UISearchBarDelegate |
| 134 | +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ |
| 135 | + NSMutableDictionary *searchResults = @{}.mutableCopy; |
| 136 | + NSString *strippedStr = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; |
| 137 | + NSArray *searchItems = [strippedStr componentsSeparatedByString:@" "]; |
| 138 | + if (strippedStr.length == 0) { |
| 139 | + searchResults = _countryCodeListDict.mutableCopy; |
| 140 | + }else{ |
| 141 | + NSMutableArray *andMatchPredicates = [NSMutableArray array]; |
| 142 | + for (NSString *searchString in searchItems){ |
| 143 | + NSMutableArray *searchItemsPredicate = [NSMutableArray new]; |
| 144 | + |
| 145 | + NSExpression *lhs = [NSExpression expressionForKeyPath:@"country"]; |
| 146 | + NSExpression *rhs = [NSExpression expressionForConstantValue:searchString]; |
| 147 | + NSPredicate *finalPredicate = [NSComparisonPredicate |
| 148 | + predicateWithLeftExpression:lhs |
| 149 | + rightExpression:rhs |
| 150 | + modifier:NSDirectPredicateModifier |
| 151 | + type:NSContainsPredicateOperatorType |
| 152 | + options:NSCaseInsensitivePredicateOption]; |
| 153 | + [searchItemsPredicate addObject:finalPredicate]; |
| 154 | + |
| 155 | + lhs = [NSExpression expressionForKeyPath:@"country_code"]; |
| 156 | + rhs = [NSExpression expressionForConstantValue:searchString]; |
| 157 | + finalPredicate = [NSComparisonPredicate |
| 158 | + predicateWithLeftExpression:lhs |
| 159 | + rightExpression:rhs |
| 160 | + modifier:NSDirectPredicateModifier |
| 161 | + type:NSContainsPredicateOperatorType |
| 162 | + options:NSCaseInsensitivePredicateOption]; |
| 163 | + [searchItemsPredicate addObject:finalPredicate]; |
| 164 | + |
| 165 | + lhs = [NSExpression expressionForKeyPath:@"iso_code"]; |
| 166 | + rhs = [NSExpression expressionForConstantValue:searchString]; |
| 167 | + finalPredicate = [NSComparisonPredicate |
| 168 | + predicateWithLeftExpression:lhs |
| 169 | + rightExpression:rhs |
| 170 | + modifier:NSDirectPredicateModifier |
| 171 | + type:NSContainsPredicateOperatorType |
| 172 | + options:NSCaseInsensitivePredicateOption]; |
| 173 | + [searchItemsPredicate addObject:finalPredicate]; |
| 174 | + |
| 175 | + NSCompoundPredicate *orMatchPredicates = (NSCompoundPredicate *)[NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate]; |
| 176 | + [andMatchPredicates addObject:orMatchPredicates]; |
| 177 | + } |
| 178 | + NSCompoundPredicate *finalCompoundPredicate = (NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates]; |
| 179 | + for (NSString *key in [_countryCodeListDict allKeys]) { |
| 180 | + NSArray *finalList = [_countryCodeListDict[key] filteredArrayUsingPredicate:finalCompoundPredicate]; |
| 181 | + if (finalList.count > 0) { |
| 182 | + [searchResults setValue:finalList forKey:key]; |
| 183 | + }else{ |
| 184 | + [searchResults removeObjectForKey:key]; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + _searchResults = searchResults; |
| 189 | + [self p_updateKeyList]; |
| 190 | + [self.myTableView reloadData]; |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | +@end |
0 commit comments