replace deprecated UIsearchDisplayControl by UISearchControl

This commit is contained in:
Danmei Chen 2020-03-05 11:51:53 +01:00
parent 6a62f35fff
commit 2e2f1dcb73
3 changed files with 35 additions and 35 deletions

View file

@ -1,35 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15702" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15704"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="CountryListView">
<connections>
<outlet property="searchDisplayController" destination="Fzt-cO-ZZd" id="yWw-yG-tfg"/>
<outlet property="tableView" destination="UcW-gD-iwL" id="878-PR-Gn9"/>
<outlet property="view" destination="1" id="3"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="1">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view alpha="0.90000000000000002" tag="2" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="418-8u-hzT" userLabel="topBar">
<rect key="frame" x="0.0" y="0.0" width="375" height="66"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="66"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" tag="5" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="SELECT A COUNTRY" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MLt-gO-M3J" userLabel="titleLabel">
<rect key="frame" x="83" y="0.0" width="209" height="66"/>
<rect key="frame" x="92" y="0.0" width="230" height="66"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" heightSizable="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<color key="textColor" red="0.98766469955444336" green="0.27512490749359131" blue="0.029739789664745331" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" tag="6" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="hOj-Kl-K9W" userLabel="backButton" customClass="UIInterfaceStyleButton">
<rect key="frame" x="0.0" y="0.0" width="75" height="66"/>
<rect key="frame" x="0.0" y="0.0" width="82" height="66"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" heightSizable="YES"/>
<accessibility key="accessibilityConfiguration" label="Back"/>
<inset key="titleEdgeInsets" minX="0.0" minY="18" maxX="0.0" maxY="0.0"/>
@ -46,7 +47,7 @@
<color key="backgroundColor" systemColor="secondarySystemBackgroundColor" red="0.94901960780000005" green="0.94901960780000005" blue="0.96862745100000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<tableView clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" alwaysBounceVertical="YES" style="plain" rowHeight="40" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="UcW-gD-iwL">
<rect key="frame" x="0.0" y="109" width="375" height="558"/>
<rect key="frame" x="0.0" y="66" width="414" height="830"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<color key="separatorColor" red="0.66666666666666663" green="0.66666666666666663" blue="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
@ -56,28 +57,11 @@
<outlet property="delegate" destination="-1" id="Dg5-gz-Qgm"/>
</connections>
</tableView>
<searchBar contentMode="redraw" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ipz-Nn-2z3">
<rect key="frame" x="0.0" y="65" width="375" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="-1" id="iL7-Xf-Ila"/>
</connections>
</searchBar>
</subviews>
<color key="backgroundColor" red="0.93620026111602783" green="0.94070994853973389" blue="0.95069998502731323" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="simulatedStatusBarMetrics"/>
<point key="canvasLocation" x="134" y="143"/>
</view>
<searchDisplayController id="Fzt-cO-ZZd">
<connections>
<outlet property="delegate" destination="-1" id="TiS-Oi-O2O"/>
<outlet property="searchBar" destination="Ipz-Nn-2z3" id="MMD-qU-wyt"/>
<outlet property="searchContentsController" destination="-1" id="CXr-Rc-c3m"/>
<outlet property="searchResultsDataSource" destination="-1" id="D0N-dF-fAE"/>
<outlet property="searchResultsDelegate" destination="-1" id="hKg-ZM-V4D"/>
</connections>
</searchDisplayController>
</objects>
<resources>
<image name="back_default.png" width="38.400001525878906" height="35.200000762939453"/>

View file

@ -20,13 +20,14 @@
#import <UIKit/UIKit.h>
#import "PhoneMainView.h"
@protocol CountryListViewDelegate <NSObject,UISearchDisplayDelegate,UISearchBarDelegate>
@protocol CountryListViewDelegate <NSObject>
- (void)didSelectCountry:(NSDictionary *)country;
@end
@interface CountryListView : UIViewController<UICompositeViewDelegate>
@interface CountryListView : UIViewController<UICompositeViewDelegate,UISearchResultsUpdating,UISearchBarDelegate>
@property (nonatomic, weak) id<CountryListViewDelegate>delegate;
@property(strong, nonatomic) UISearchController *searchController;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope;

View file

@ -75,9 +75,22 @@ static UICompositeViewDescription *compositeDescription = nil;
[super viewDidLoad];
_searchResults = [[NSArray alloc] init];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.obscuresBackgroundDuringPresentation = false;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
[_tableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.searchController.active) {
self.searchController.active = NO;
[self.searchController.searchBar removeFromSuperview];
}
}
#pragma mark - UITableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
@ -85,13 +98,11 @@ static UICompositeViewDescription *compositeDescription = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView){
if (self.searchController.active){
return _searchResults.count;
}else{
return [self.class getData].count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@ -101,7 +112,7 @@ static UICompositeViewDescription *compositeDescription = nil;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
if (self.searchController.active) {
cell.textLabel.text = [[_searchResults objectAtIndex:indexPath.row] valueForKey:@"name"];
cell.detailTextLabel.text = [[_searchResults objectAtIndex:indexPath.row] valueForKey:@"code"];
}else{
@ -117,7 +128,7 @@ static UICompositeViewDescription *compositeDescription = nil;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([_delegate respondsToSelector:@selector(didSelectCountry:)]) {
NSDictionary* dict = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
if (self.searchController.active) {
dict = [_searchResults objectAtIndex:indexPath.row];
}else{
dict = [[self.class getData] objectAtIndex:indexPath.row];
@ -128,6 +139,15 @@ static UICompositeViewDescription *compositeDescription = nil;
[PhoneMainView.instance popCurrentView];
}
#pragma mark - searchController delegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self filterContentForSearchText:self.searchController.searchBar.text scope:@""];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
#pragma mark - Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
@ -139,11 +159,6 @@ static UICompositeViewDescription *compositeDescription = nil;
[PhoneMainView.instance popCurrentView];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
+ (NSDictionary *)countryWithIso:(NSString *)iso {
for (NSDictionary *dict in [self.class getData]) {
if ([[dict objectForKey:@"iso"] isEqualToString:iso.uppercaseString]) {