当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS tableview实现简单搜索功能

iOS tableview实现简单搜索功能

2019年07月24日  | 移动技术网IT编程  | 我要评论

天鹅之死吉他谱,爽口乐多少钱,豫南花鼓戏

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

一、先用xcode创建好工程

通过xib文件来初始化视图控制器

二、编写代码

1、先为nsdictionary创建一个分类 实现字典的深拷贝

.h文件

#import <foundation/foundation.h>

@interface nsdictionary (mutabledeepcopy)

- (nsmutabledictionary *)mutabledeepcopy;
@end


.m文件

#import "nsdictionary+mutabledeepcopy.h"

@implementation nsdictionary (mutabledeepcopy)

- (nsmutabledictionary *)mutabledeepcopy
{
 nsmutabledictionary *mutabledictionary = [nsmutabledictionary dictionarywithcapacity:[self count]]; //这里的容量也只是个参考值,表示对大小的限制 大小是调用该方法的count
 nsarray *keys = [self allkeys]; //self就是个可变的字典
 for(id key in keys)
 {
  id dicvalue = [self valueforkey:key]; 
  //从 nsdictionary 取值的时候有两个方法objectforkey valueforkey
  id diccopy = nil;
  if([dicvalue respondstoselector:@selector(mutabledeepcopy)]) 
  //如果对象没有响应mutabledeepcopy 就创建一个可变副本 dicvalue 有没有实现这个方法
  {
   diccopy = [dicvalue mutabledeepcopy];
  }
  else if([dicvalue respondstoselector:@selector(mutablecopy)])
  {
   diccopy = [dicvalue mutablecopy];
  }
  if(diccopy ==nil)
  {
   diccopy = [dicvalue copy];
  }
  [mutabledictionary setvalue:diccopy forkey:key];
 }
 return mutabledictionary;
}
@end

2、编写主代码

.h文件
notescanviewcontroller.h

#import <uikit/uikit.h>

@interface notescanviewcontroller : uiviewcontroller <uitableviewdatasource,uitableviewdelegate,uisearchbardelegate>

@property (nonatomic,retain)nsmutabledictionary *words;
@property (nonatomic,retain)nsmutablearray *keys;
@property (weak, nonatomic) iboutlet uitableview *table;
@property (weak, nonatomic) iboutlet uisearchbar *search;

@property (nonatomic,retain)nsdictionary *allwords;
- (void)resetsearch;
- (void)handlesearchforterm:(nsstring *)searchterm;

@end

.m文件

#import "notescanviewcontroller.h"
#import "nsdictionary+mutabledeepcopy.h"
@interface notescanviewcontroller ()

@end

@implementation notescanviewcontroller

- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil
{
 self = [super initwithnibname:nibnameornil bundle:nibbundleornil];
 if (self) {
  // custom initialization
 }
 return self;
}


- (void)viewdidload //只在第一次加载视图调用
{
 [super viewdidload];
 /*加载plist文件*/
 nsstring *wordspath = [[nsbundle mainbundle]pathforresource:@"notesection" oftype:@"plist"];//得到属性列表的路径
 nsdictionary *dictionary = [[nsdictionary alloc]initwithcontentsoffile:wordspath];
 self.allwords = dictionary;
 [self resetsearch]; //加载并填充words可变字典和keys数组
 
 _search.autocapitalizationtype = uitextautocapitalizationtypenone;//不自动大写
 _search.autocorrectiontype = uitextautocorrectiontypeno;//不自动纠错
}

//取消搜索或者改变搜索条件
- (void)resetsearch
{
 self.words = [self.allwords mutabledeepcopy]; //得到所有字典的副本 得到一个字典
 nslog(@"所有字典 = %@",self.words);
 nsmutablearray *keyarray = [[nsmutablearray alloc]init];//创建一个可变数组
 [keyarray addobjectsfromarray:[[self.allwords allkeys]sortedarrayusingselector:@selector(compare:)]]; //用指定的selector对array的元素进行排序
 self.keys = keyarray; //将所有key 存到一个数组里面
 nslog(@"所有key = %@",self.keys);
}
//实现搜索方法
- (void)handlesearchforterm:(nsstring *)searchterm
{
 nsmutablearray *sectionsremove = [[nsmutablearray alloc]init]; //创建一个数组存放我们所找到的空分区
 [self resetsearch];
 for(nsstring *key in self.keys)//遍历所有的key
 {
  nsmutablearray *array = [_words valueforkey:key] ;  //得到当前键key的名称 数组
  nsmutablearray *toremove = [[nsmutablearray alloc]init];//需要从words中删除的值 数组
  for(nsstring *word in array) //实现搜索
  {
   if([word rangeofstring:searchterm options:nscaseinsensitivesearch].location == nsnotfound)//搜索时忽略大小写 把没有搜到的值 放到要删除的对象数组中去
    [toremove addobject:word]; //把没有搜到的内容放到 toremove中去
  }
  
  if([array count] == [toremove count])//校对要删除的名称数组长度和名称数组长度是否相等
   [sectionsremove addobject:key]; //相等 则整个分区组为空
  [array removeobjectsinarray:toremove]; //否则 删除数组中所有与数组toremove包含相同的元素
 }
 [self.keys removeobjectsinarray:sectionsremove];// 删除整个key 也就是删除空分区,释放用来存储分区的数组,并重新加载table 这样就实现了搜索
 [_table reloaddata];
}

- (void)viewwillappear:(bool)animated //当使用push或者prenset方式调用
{
}
//#pragma mark -
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
 return ([_keys count] >0)?[_keys count]:1; //搜索时可能会删除所有分区 则要保证要有一个分区
}

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
 if([_keys count] == 0)
 {
  return 0;
 }
 nsstring *key = [_keys objectatindex:section]; //得到第几组的key
 nsarray *wordsection = [_words objectforkey:key]; //得到这个key里面所有的元素
 return [wordsection count]; //返回元素的个数
}
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
 nsuinteger section = [indexpath section]; //得到第几组
 nsuinteger row = [indexpath row]; //得到第几行
 nsstring *key = [_keys objectatindex:section]; //得到第几组的key
 nsarray *wordsection = [_words objectforkey:key]; //得到这个key里面的所有元素
 static nsstring *notesectionidentifier = @"notesectionidentifier";
 uitableviewcell *cell =[tableview dequeuereusablecellwithidentifier:notesectionidentifier];
 if(cell == nil)
 {
  cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:notesectionidentifier];
  
 }
 cell.textlabel.text = [wordsection objectatindex:row];
 return cell;
}
//为每个分区指定一个标题
- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section
{
 if([_keys count] == 0)
  return @" ";
 nsstring *key = [_keys objectatindex:section];
 return key;
}
//创建一个索引表
- (nsarray *)sectionindextitlesfortableview:(uitableview *)tableview
{
 return _keys;
}
#pragma mark - 

- (nsindexpath *)tableview:(uitableview *)tableview willselectrowatindexpath:(nsindexpath *)indexpath
{
 [_search resignfirstresponder]; //点击任意 cell都会取消键盘
 return indexpath;
}

#pragma mark-

- (void)searchbarsearchbuttonclicked:(uisearchbar *)searchbar //搜索button点击事件
{
 nsstring *searchterm = [searchbar text];
 [self handlesearchforterm:searchterm]; //搜索内容 删除words里面的空分区和不匹配内容
}

- (void)searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext
{ //搜索内容随着输入及时地显示出来
 if([searchtext length] == 0)
 {
  [self resetsearch];
  [_table reloaddata];
  return;
 }
 else
  [self handlesearchforterm:searchtext];
}

- (void)searchbarcancelbuttonclicked:(uisearchbar *)searchbar //点击取消按钮
{
 _search.text = @""; //标题 为空
 [self resetsearch]; //重新 加载分类数据
 [_table reloaddata];
 [searchbar resignfirstresponder]; //退出键盘

}
@end

运行结果

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网