热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

iOStableView实现搜索功能

这篇文章主要为大家详细介绍了iOStableView实现搜索功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了tableView搜索功能的具体代码,供大家参考,具体内容如下

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

github里面有详细的说明

支持中文排序

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

#define reusedID @"cityCell"

@interface WLCCityTVC () 
/**
 * 存放城市字典的数组,后来也存放排序后的
 */
@property (nonatomic, strong) NSMutableArray* cityArrM;
@property (nonatomic, strong) WLCUser* user;
/**
 * 存放首字母的数组
 */
@property (nonatomic, strong) NSMutableArray* letterArrM;
@property (nonatomic, strong) NSMutableArray* wordArr;
/**
 * 存放城市模型的数组
 */
@property (nonatomic, strong) NSMutableArray* modelArrM;
@property (strong, nonatomic) NSMutableArray* searchDataSource; /**<搜索结果数据源*/
@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];

 //根据拼音对数组排序
 NSArray* sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"letter" ascending:YES]];
 //排序
 [arrayToSort sortUsingDescriptors:sortDescriptors];

 NSMutableArray* tempArray = nil;
 BOOL flag = NO;

 //分组
 for (int i = 0; i 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
书友36296361
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有