最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文實例為大家分享了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:@"獲取省份失敗,請稍后重試"];
   [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ù)拼音對數(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下劃線左對齊
- (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];
 //對排序好的數(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

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

相關(guān)文章

  • Navigation bar的注意事項詳解

    Navigation bar的注意事項詳解

    本文主要介紹了Navigation bar的注意事項。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • ios uicollectionview實現(xiàn)橫向滾動

    ios uicollectionview實現(xiàn)橫向滾動

    這篇文章主要為大家詳細(xì)介紹了ios uicollectionview實現(xiàn)橫向滾動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • iOS頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)

    iOS頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)

    本文主要介紹了iOS頁面跳轉(zhuǎn)的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • iOS仿微信添加標(biāo)簽效果(shape實現(xiàn))

    iOS仿微信添加標(biāo)簽效果(shape實現(xiàn))

    微信做的用戶體驗非常棒,今天用shape來做下微信的標(biāo)簽功能,非常不錯,對ios 仿微信添加標(biāo)簽功能感興趣的朋友一起看看吧
    2016-11-11
  • IOS NSTimeInterval使用案例詳解

    IOS NSTimeInterval使用案例詳解

    這篇文章主要介紹了IOS NSTimeInterval使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • IOS開發(fā)之路--C語言構(gòu)造類型

    IOS開發(fā)之路--C語言構(gòu)造類型

    在第一節(jié)中我們就提到C語言的構(gòu)造類型,分為:數(shù)組、結(jié)構(gòu)體、枚舉、共用體,當(dāng)然前面數(shù)組的內(nèi)容已經(jīng)說了很多了,這一節(jié)將會重點說一下其他三種類型。
    2014-08-08
  • IOS圖片設(shè)置毛玻璃效果

    IOS圖片設(shè)置毛玻璃效果

    這篇文章主要介紹了IOS圖片設(shè)置毛玻璃效果的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • iOS繪制專屬于程序猿的浪漫愛心

    iOS繪制專屬于程序猿的浪漫愛心

    誰說程序猿不懂浪漫,這篇文章主要介紹了iOS繪制專屬于程序猿的浪漫愛心,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS實現(xiàn)帶文字的圓形頭像效果

    iOS實現(xiàn)帶文字的圓形頭像效果

    隨著騰訊QQ的普及,現(xiàn)在越來越多的社交類APP在顯示頭像的時候,都選擇了圓形頭像,本文將更進(jìn)一步的介紹如何實現(xiàn)帶文字的圓形頭像效果,效果非常不錯,感興趣的朋友們可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • iOS中tableView cell分割線的一些設(shè)置技巧

    iOS中tableView cell分割線的一些設(shè)置技巧

    在項目開發(fā)中我們會常常遇到tableView 的cell分割線顯示不全,左邊會空出一截像素,更有甚者想改變系統(tǒng)的分割線,下面通過這篇文章來一起學(xué)習(xí)學(xué)習(xí)在iOS中tableView cell分割線的一些設(shè)置技巧,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05

最新評論

濮阳市| 隆子县| 顺义区| 青州市| 甘谷县| 米易县| 永善县| 本溪| 东丽区| 伊通| 大新县| 芜湖市| 阿勒泰市| 哈密市| 贞丰县| 鹤壁市| 吕梁市| 大石桥市| 阿克陶县| 洪湖市| 甘泉县| 建昌县| 武夷山市| 玛曲县| 保德县| 将乐县| 理塘县| 鄯善县| 河北省| 乌审旗| 泰州市| 桐城市| 新建县| 河池市| 梅州市| 桂林市| 洪雅县| 西畴县| 景谷| 疏勒县| 游戏|