2017-07-04 16_40_08.gif
主界面代码:
#import "ViewController.h"
#import "CountryCodeViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 50, 25)];
self.label = codeLabel;
codeLabel.font = [UIFont systemFontOfSize:17];
codeLabel.textColor = [UIColor colorWithRed:0 green:255 blue:127 alpha:0.7];
codeLabel.text = @"+86";
[self.view addSubview:codeLabel];
UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
codeBtn.frame = CGRectMake(100, 100, 50, 25);
[codeBtn addTarget:self action:@selector(chooseCountryCode) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:codeBtn];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)chooseCountryCode{
CountryCodeViewController *CCVC = [[CountryCodeViewController alloc] init];
CCVC.selectBlock = ^(NSDictionary *dic) {
self.label.text = [NSString stringWithFormat:@"+%@",dic[@"country_code"]];
};
[self.navigationController pushViewController:CCVC animated:YES];
}
@end
cell代码
#import <UIKit/UIKit.h>
@interface CountryCodeCell : UITableViewCell
@property(nonatomic,strong)UILabel* CountryLable;
@property(nonatomic,strong)UILabel* CodeLable;
@property(nonatomic,strong)NSDictionary* CountryDic;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
@end
#import "CountryCodeCell.h"
@implementation CountryCodeCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_CountryLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100,
self.contentView.bounds.size.height)];
_CountryLable.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:_CountryLable];
_CodeLable = [[UILabel alloc] initWithFrame:CGRectMake(self.contentView.bounds.size.width-20, 5, 50, self.contentView.bounds.size.height)];
_CodeLable.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:_CodeLable];
}
return self;
}
-(void)setCountryDic:(NSDictionary *)CountryDic{
_CountryDic = CountryDic;
self.CountryLable.text = _CountryDic[@"country"];
self.CodeLable.text = [NSString stringWithFormat:@"+%@",_CountryDic[@"country_code"]];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
TableView数据源格式
image.png
选择界面TableView代码
#import <UIKit/UIKit.h>
@interface CountryCodeViewController : UIViewController
@property (copy,nonatomic) void (^selectBlock)(NSDictionary*);
@end
#define kScreen_Width [UIScreen mainScreen].bounds.size.width
#import "CountryCodeViewController.h"
#import "CountryCodeCell.h"
@interface CountryCodeViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UISearchBar *searchBar;
@property (strong, nonatomic) NSDictionary *countryCodeListDict, *searchResults;
@property (strong, nonatomic) NSMutableArray *indexList;
@end
@implementation CountryCodeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"选择国家或地区";
_tableView = ({
UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableview.delegate = self;
tableview.dataSource = self;
[tableview registerClass:[CountryCodeCell class] forCellReuseIdentifier:@"codeCell"];
[self.view addSubview:tableview];
tableview;
});
_searchBar = ({
UISearchBar *searchbar = [UISearchBar new];
searchbar.delegate = self;
searchbar.placeholder = @"国家/地区名称";
[searchbar sizeToFit];
searchbar;
});
self.tableView.tableHeaderView = _searchBar;
[self initData];
}
- (void)initData{
NSString *path = [[NSBundle mainBundle] pathForResource:@"country" ofType:@"plist"];
self.countryCodeListDict = self.searchResults = [NSDictionary dictionaryWithContentsOfFile:path];
[self initIndexList];
}
- (void)initIndexList{
self.indexList = [self.searchResults.allKeys mutableCopy];
[_indexList sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 isEqualToString:@"#"]) {
return NSOrderedAscending;
}else if([obj2 isEqualToString:@"#"]){
return NSOrderedDescending;
}else{
return [obj1 compare:obj2];
}
}];
[_indexList insertObject:UITableViewIndexSearch atIndex:0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.indexList.count -1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.searchResults[self.indexList[section+1]] count];
}
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.indexList;
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index >0? index-1: index;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headView = [UIView new];
headView.backgroundColor = [UIColor colorWithRed:240/255.0 green:255/255.0 blue:255/255.0 alpha:1];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 40, 10)];
titleLabel.font = [UIFont systemFontOfSize:12];
titleLabel.text = [self.indexList[section+1] isEqualToString:@"#"]? @"常用": self.indexList[section+1];
[headView addSubview:titleLabel];
return headView;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CountryCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"codeCell" forIndexPath:indexPath];
cell.CountryDic = self.searchResults[self.indexList[indexPath.section+1]][indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (_selectBlock) {
_selectBlock(_searchResults[_indexList[indexPath.section+ 1]][indexPath.row]);
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, (kScreen_Width - cell.contentView.bounds.size.width) + 15);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == self.tableView) {
[self.searchBar resignFirstResponder];
}
}
#pragma mark - searchBar delegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSMutableDictionary *searchResults = @{}.mutableCopy;
NSString *strippedStr = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *searchItems = [strippedStr componentsSeparatedByString:@" "];
if (strippedStr.length==0) {
searchResults = self.countryCodeListDict.mutableCopy;
}else{
NSMutableArray *andMatchPredicates = [NSMutableArray new];
for (NSString *searchString in searchItems) {
NSMutableArray *searchItemsPredicate = [NSMutableArray new];
NSExpression *lex = [NSExpression expressionForKeyPath:@"country"];
NSExpression *rex = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicate = [NSComparisonPredicate predicateWithLeftExpression:lex rightExpression:rex modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
lex = [NSExpression expressionForKeyPath:@"country_code"];
rex = [NSExpression expressionForConstantValue:searchString];
finalPredicate = [NSComparisonPredicate predicateWithLeftExpression:lex rightExpression:rex modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
lex = [NSExpression expressionForKeyPath:@"iso_code"];
rex = [NSExpression expressionForConstantValue:searchString];
finalPredicate = [NSComparisonPredicate predicateWithLeftExpression:lex rightExpression:rex modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
NSCompoundPredicate *orMatchPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate];
[andMatchPredicates addObject:orMatchPredicate];
}
NSCompoundPredicate *finalCompoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
for (NSString *key in [self.countryCodeListDict allKeys]) {
NSArray *finalList = [self.countryCodeListDict[key] filteredArrayUsingPredicate:finalCompoundPredicate];
if (finalList.count>0) {
[searchResults setValue:finalList forKey:key];
}else{
[searchResults removeObjectForKey:key];
}
}
self.searchResults = searchResults;
[self initIndexList];
[self.tableView reloadData];
}
}
@end
网友评论