From 68d139a184738589c4c777de033a7c6c6f8c459d Mon Sep 17 00:00:00 2001 From: kai60 Date: Sat, 3 Oct 2020 11:17:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E9=A1=B5=E9=9D=A2=E5=9B=BD?= =?UTF-8?q?=E5=AE=B6=E4=BB=A3=E7=A0=81=E9=97=AA=E9=80=80=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../XWCountryCode/XWCountryCodeController.m | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/Ifish/controllers/logAddRegistController/XWCountryCode/XWCountryCodeController.m b/Ifish/controllers/logAddRegistController/XWCountryCode/XWCountryCodeController.m index 0f9130e..69335a8 100755 --- a/Ifish/controllers/logAddRegistController/XWCountryCode/XWCountryCodeController.m +++ b/Ifish/controllers/logAddRegistController/XWCountryCode/XWCountryCodeController.m @@ -12,11 +12,11 @@ #define CURR_LANG ([[NSLocale preferredLanguages] objectAtIndex:0]) #define LanguageIsEnglish ([CURR_LANG isEqualToString:@"en-US"] || [CURR_LANG isEqualToString:@"en-CA"] || [CURR_LANG isEqualToString:@"en-GB"] || [CURR_LANG isEqualToString:@"en-CN"] || [CURR_LANG isEqualToString:@"en"]) -@interface XWCountryCodeController(){ +@interface XWCountryCodeController(){ //国际代码主tableview UITableView *countryCodeTableView; //搜索 - UISearchDisplayController *searchController; + UISearchController *searchController; // UISearchController *searchController; UISearchBar *searchBar; //代码字典 @@ -53,7 +53,7 @@ -(void)creatSubviews{ searchResultValuesArray = [[NSMutableArray alloc] init]; - countryCodeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-20) style:UITableViewStylePlain]; + countryCodeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-40) style:UITableViewStylePlain]; [self.view addSubview:countryCodeTableView]; //自动调整自己的宽度,保证与superView左边和右边的距离不变。 [countryCodeTableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; @@ -61,18 +61,15 @@ [countryCodeTableView setDelegate:self]; [countryCodeTableView setSectionIndexBackgroundColor:[UIColor clearColor]]; - searchBar = [[UISearchBar alloc] init]; - [searchBar sizeToFit]; - [searchBar setDelegate:self]; - //关闭系统自动联想和首字母大写功能 - [searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone]; - [countryCodeTableView setTableHeaderView:searchBar]; - - searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; - [searchController setDelegate:self]; - searchController.searchResultsDataSource = self; - searchController.searchResultsDelegate = self; + + searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; + [searchController setSearchResultsUpdater:self]; + searchController.dimsBackgroundDuringPresentation=NO; + searchController.definesPresentationContext=NO; + searchController.searchBar.delegate=self; + countryCodeTableView.tableHeaderView=searchController.searchBar; + searchBar=searchController.searchBar; NSString *plistPathCH = [[NSBundle mainBundle] pathForResource:@"sortedChnames" ofType:@"plist"]; NSString *plistPathEN = [[NSBundle mainBundle] pathForResource:@"sortedEnames" ofType:@"plist"]; @@ -91,25 +88,29 @@ } -//搜索 --(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ +-(void)updateSearchResultsForSearchController:(UISearchController *)searchController +{ NSLog(@"%s",__FUNCTION__); [searchResultValuesArray removeAllObjects]; for (NSArray *array in [sortedNameDict allValues]) { for (NSString *value in array) { - if ([value containsString:searchBar.text]) { + if ([value containsString:searchController.searchBar.text]) { [searchResultValuesArray addObject:value]; } } } - [searchController.searchResultsTableView reloadData]; + + + [countryCodeTableView reloadData]; } + + #pragma mark - UITableView //section -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ - if (tableView == countryCodeTableView) { + if (!searchController.isActive) { return [sortedNameDict allKeys].count; }else{ return 1; @@ -117,7 +118,7 @@ } //row -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ - if (tableView == countryCodeTableView) { + if (!searchController.isActive) { NSArray *array = [sortedNameDict objectForKey:[indexArray objectAtIndex:section]]; return array.count; @@ -134,7 +135,7 @@ //初始化cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ - if (tableView == countryCodeTableView) { + if (!searchController.isActive) { static NSString *ID1 = @"cellIdentifier1"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID1]; if (!cell) { @@ -164,7 +165,7 @@ //indexTitle -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ - if (tableView == countryCodeTableView) { + if (!searchController.isActive) { return indexArray; }else{ return nil; @@ -172,7 +173,7 @@ } // -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ - if (tableView == countryCodeTableView) { + if (!searchController.isActive) { return index; }else{ return 0; @@ -181,7 +182,7 @@ - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { - if (tableView == countryCodeTableView) { + if (!searchController.isActive) { if (section == 0) { return 0; } @@ -212,6 +213,9 @@ if (self.returnCountryCodeBlock != nil) { self.returnCountryCodeBlock(cell.textLabel.text); } + if (searchController.isActive) { + [searchController dismissViewControllerAnimated:YES completion:nil]; + } [self dismissViewControllerAnimated:YES completion:nil]; }