欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS tableView實(shí)現(xiàn)搜索功能

 更新時(shí)間:2017年11月30日 14:16:13   作者:為大家詳細(xì)  
這篇文章主要為大家詳細(xì)介紹了iOS tableView實(shí)現(xiàn)搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了tableView搜索功能的具體代碼,供大家參考,具體內(nèi)容如下

框架:https://github.com/honeycao/HCSortAndSearchDemo

github里面有詳細(xì)的說明

支持中文排序

#import "ChineseToPinyin.h"
#import "HCSortString.h"
#import "WLCCityTVC.h"
#import "WLCProvinceModel.h"
#import "ZYPinYinSearch.h"

#define reusedID @"cityCell"

@interface WLCCityTVC () <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating>
/**
 * 存放城市字典的數(shù)組,后來也存放排序后的
 */
@property (nonatomic, strong) NSMutableArray* cityArrM;
@property (nonatomic, strong) WLCUser* user;
/**
 * 存放首字母的數(shù)組
 */
@property (nonatomic, strong) NSMutableArray* letterArrM;
@property (nonatomic, strong) NSMutableArray* wordArr;
/**
 * 存放城市模型的數(shù)組
 */
@property (nonatomic, strong) NSMutableArray* modelArrM;
@property (strong, nonatomic) NSMutableArray* searchDataSource; /**<搜索結(jié)果數(shù)據(jù)源*/
@property (strong, nonatomic) UISearchController* searchController;
@end

@implementation WLCCityTVC

- (void)viewDidLoad
{
 [super viewDidLoad];
 [self setupUI];
}

#pragma mark - navitionBarBackBarItem返回事件
- (BOOL)navigationShouldPopOnBackButton
{
 self.searchController.active = NO;
 return YES;
}


- (void)didReceiveMemoryWarning
{
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}

- (WLCUser*)user
{
 if (nil == _user) {
  _user = [NSKeyedUnarchiver unarchiveObjectWithFile:fileUser];
 }
 return _user;
}

- (NSMutableArray*)cityArrM
{
 if (nil == _cityArrM) {
  _cityArrM = [NSMutableArray array];
 }
 return _cityArrM;
}

- (NSMutableArray*)wordArr
{
 if (nil == _wordArr) {
  _wordArr = [NSMutableArray array];
 }
 return _wordArr;
}

- (NSMutableArray*)letterArrM
{
 if (nil == _letterArrM) {
  _letterArrM = [NSMutableArray array];
 }
 return _letterArrM;
}

- (UISearchController*)searchController
{
 if (!_searchController) {
  _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  _searchController.searchResultsUpdater = self;
  _searchController.dimsBackgroundDuringPresentation = NO;
  _searchController.hidesNavigationBarDuringPresentation = NO;
  _searchController.searchBar.placeholder = @"搜索";
    _searchController.searchBar.tintColor = [UIColor whiteColor];
  [_searchController.searchBar sizeToFit];
 }
 return _searchController;
}

- (NSMutableArray*)modelArrM
{
 if (nil == _modelArrM) {
  _modelArrM = [NSMutableArray array];
  for (NSArray* tempArr in self.cityArrM) {
   for (NSDictionary* dict in tempArr) {
    [WLCProvinceModel setupReplacedKeyFromPropertyName:^NSDictionary* {
     return @{
      @"pID" : @"id"
     };
    }];
    WLCProvinceModel* model = [WLCProvinceModel objectWithKeyValues:dict];
    [_modelArrM addObject:model];
   }
  }
 }
 return _modelArrM;
}

- (NSMutableArray*)searchDataSource
{
 if (nil == _searchDataSource) {
  _searchDataSource = [NSMutableArray array];
 }
 return _searchDataSource;
}

- (void)setupUI
{
 self.tableView.backgroundColor = [UIColor rgb:234 andGreen:234 andBlue:243];
 [self getAllCities];
 self.tableView.delegate = self;
 self.tableView.dataSource = self;
 self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)getAllCities
{
 NSString* url = [kURL stringByAppendingString:@"promary/"];
 url = [url stringByAppendingString:self.cityID];
 url = [url stringByAppendingString:@"/city"];
 [SVProgressHUD showWithStatus:@"獲取城市中"];
 [NetRequestTool requestWithParamsDict:nil image:nil name:nil token:self.user.token value:nil hearerField:nil URL:url type:GET successBlock:^(AFHTTPRequestOperation* _Nonnull operation, id _Nonnull responseObject) {
  NSString* errNum = [NSString stringWithFormat:@"%@", responseObject[@"errNum"]];
  if ([errNum isEqualToString:@"1"]) {
   [SVProgressHUD setMinimumDismissTimeInterval:2.5];
   [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"%@", responseObject[@"retMsg"]]];
   [self.navigationController popViewControllerAnimated:YES];
  }
  else {
   [SVProgressHUD dismiss];
   @try {
    self.cityArrM = responseObject[@"retData"];
    NSMutableArray* cityArrM = [NSMutableArray array];
    for (NSMutableDictionary* cityDic in self.cityArrM) {
     [cityArrM addObject:[cityDic objectForKey:@"name"]];
     NSMutableDictionary* dictM = [NSMutableDictionary dictionaryWithDictionary:cityDic];
     [self prepareCityListDatasourceWithArray:cityArrM andToDictionary:dictM];
    }
    self.cityArrM = [self sortArray:self.wordArr];

   } @catch (NSException* exception) {

   } @finally {
    [self.tableView setTableHeaderView:self.searchController.searchBar];
    [self.tableView reloadData];
   }
  }

 }
  anderrorBlock:^(AFHTTPRequestOperation* _Nonnull operation, NSError* _Nonnull error) {
   WLog(@"error == %@", error);
   [SVProgressHUD showErrorWithStatus:@"獲取省份失敗,請(qǐng)稍后重試"];
   [self.navigationController popViewControllerAnimated:YES];
  }];
}

#pragma mark -排序城市
- (void)prepareCityListDatasourceWithArray:(NSArray*)array andToDictionary:(NSMutableDictionary*)dic
{
 for (NSString* city in array) {

  NSString* cityPinyin = [ChineseToPinyin pinyinFromChiniseString:city];
  if ([city isEqualToString:@"重慶"]) {
   cityPinyin = @"CHONGQING";
  }

  NSString* firstLetter = [cityPinyin substringWithRange:NSMakeRange(0, 1)];

  if (![dic objectForKey:firstLetter]) {
   //   NSMutableArray* arr = [NSMutableArray array];
   //   [dic setValue:firstLetter forKey:@"letter"];
   dic[@"letter"] = firstLetter;
  }
  if ([[dic objectForKey:firstLetter] containsObject:city]) {
   return;
  }
 }

 [self.wordArr addObject:dic];

 //  [self.wordArr addObjectsFromArray:[[dic allValues] sortedArrayUsingSelector:@selector(compare:)]];
}

/**
 * 排序并按首字母分組
 *
 * @param arrayToSort <#arrayToSort description#>
 *
 * @return <#return value description#>
 */
- (NSMutableArray*)sortArray:(NSMutableArray*)arrayToSort
{
 NSMutableArray* arrayForArrays = [[NSMutableArray alloc] init];

 //根據(jù)拼音對(duì)數(shù)組排序
 NSArray* sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"letter" ascending:YES]];
 //排序
 [arrayToSort sortUsingDescriptors:sortDescriptors];

 NSMutableArray* tempArray = nil;
 BOOL flag = NO;

 //分組
 for (int i = 0; i < arrayToSort.count; i++) {
  NSString* pinyin = [arrayToSort[i] objectForKey:@"letter"];
  NSString* firstChar = [pinyin substringToIndex:1];
  //  NSLog(@"%@",firstChar);
  if (![self.letterArrM containsObject:[firstChar uppercaseString]]) {
   [self.letterArrM addObject:[firstChar uppercaseString]];
   tempArray = [[NSMutableArray alloc] init];
   flag = NO;
  }
  if ([self.letterArrM containsObject:[firstChar uppercaseString]]) {
   [tempArray addObject:arrayToSort[i]];
   if (flag == NO) {
    [arrayForArrays addObject:tempArray];
    flag = YES;
   }
  }
 }

 return arrayForArrays;
}

//讓cell下劃線左對(duì)齊
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
 if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
 }
 if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  [cell setPreservesSuperviewLayoutMargins:NO];
 }
 if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  [cell setLayoutMargins:UIEdgeInsetsZero];
 }
}

#pragma mark - tableView's delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
 if (!self.searchController.active) {
  return self.letterArrM.count;
 }
 else {
  return 1;
 }
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
 if (!self.searchController.active) {
  return [self.cityArrM[section] count];
 }
 else {
  return self.searchDataSource.count;
 }
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
 UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reusedID];
 if (!cell) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedID];
 }
 @try {
  if (!self.searchController.active) {
   NSDictionary* dict = self.cityArrM[indexPath.section][indexPath.row];
   cell.textLabel.text = [NSString stringWithFormat:@"%@", dict[@"name"]];
  }
  else {
   //   NSDictionary* dict = self.searchDataSource[indexPath.row];
   WLCProvinceModel* model = self.searchDataSource[indexPath.row];
   cell.textLabel.text = [NSString stringWithFormat:@"%@", model.name];
  }
 } @catch (NSException* exception) {

 } @finally {
 }
 return cell;
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
 if (!self.searchController.active) {
  return [self.letterArrM objectAtIndex:section];
 }
 else
  return nil;
}
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
 if (!self.searchController.active) {
  return self.letterArrM;
 }
 else
  return nil;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{

 NSDictionary* dict;
 @try {
  if (!self.searchController.active) {
   dict = self.cityArrM[indexPath.section][indexPath.row];
  }
  else {
   WLCProvinceModel* model = self.searchDataSource[indexPath.row];
   dict = [NSDictionary dictionaryWithObjects:@[ model.name, model.pID ] forKeys:@[ @"name", @"id" ]];
  }

  [[NSNotificationCenter defaultCenter] postNotificationName:@"cityChoosed" object:self userInfo:dict];
 } @catch (NSException* exception) {
 } @finally {
 }
 self.searchController.active = NO;
 [self.navigationController popToViewController:self.fatherVC animated:YES];
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UISearchDelegate
- (void)updateSearchResultsForSearchController:(UISearchController*)searchController
{
 [self.searchDataSource removeAllObjects];
 NSArray* ary = [NSArray new];
 //對(duì)排序好的數(shù)據(jù)進(jìn)行搜索
 NSDictionary* allDataSource = [HCSortString sortAndGroupForArray:self.modelArrM PropertyName:@"name"];
 ary = [HCSortString getAllValuesFromDict:allDataSource];

 if (searchController.searchBar.text.length == 0) {
  [self.searchDataSource addObjectsFromArray:ary];
 }
 else {
  ary = [ZYPinYinSearch searchWithOriginalArray:ary andSearchText:searchController.searchBar.text andSearchByPropertyName:@"name"];
  [self.searchDataSource addObjectsFromArray:ary];
 }
 [self.tableView reloadData];
}

@end

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS開發(fā)中使用UIDynamic來捕捉動(dòng)畫組件的重力行為

    iOS開發(fā)中使用UIDynamic來捕捉動(dòng)畫組件的重力行為

    這篇文章主要介紹了iOS開發(fā)中使用UIDynamic來捕捉動(dòng)畫組件的重力行為的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-12-12
  • iOS觸摸事件UITouch應(yīng)用詳解

    iOS觸摸事件UITouch應(yīng)用詳解

    這篇文章主要為大家詳細(xì)介紹了iOS觸摸事件UITouch的應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • iOS json解析出錯(cuò)的幾種情況總結(jié)

    iOS json解析出錯(cuò)的幾種情況總結(jié)

    這篇文章主要介紹了iOS json解析出錯(cuò)的幾種情況總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 用iOS模擬器安裝App的方法

    用iOS模擬器安裝App的方法

    下面小編就為大家分享一篇用iOS模擬器安裝App的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息

    iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息

    這篇文章主要介紹了iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度

    iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度

    這篇文章主要為大家詳細(xì)介紹了iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • ARM匯編逆向iOS 實(shí)戰(zhàn)

    ARM匯編逆向iOS 實(shí)戰(zhàn)

    本文給大家通過一些簡(jiǎn)單的實(shí)戰(zhàn)來講解使用ARM匯編進(jìn)行IOS逆向工程的方法,十分的實(shí)用,有需要的小伙伴可以參考下
    2015-11-11
  • 詳解iOS Method Swizzling使用陷阱

    詳解iOS Method Swizzling使用陷阱

    這篇文章主要介紹了詳解iOS Method Swizzling使用陷阱,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • ios使用NSProxy實(shí)現(xiàn)消息轉(zhuǎn)發(fā)

    ios使用NSProxy實(shí)現(xiàn)消息轉(zhuǎn)發(fā)

    本文主要介紹了ios使用NSProxy實(shí)現(xiàn)消息轉(zhuǎn)發(fā),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • ios開發(fā)加載webview顯示進(jìn)度條實(shí)例

    ios開發(fā)加載webview顯示進(jìn)度條實(shí)例

    本篇文章主要介紹了ios開發(fā)加載webview顯示進(jìn)度條實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05

最新評(píng)論