iOS tableview實(shí)現(xiàn)簡(jiǎn)單搜索功能
本文實(shí)例為大家分享了tableview實(shí)現(xiàn)搜索功能的具體代碼,供大家參考,具體內(nèi)容如下
一、先用xcode創(chuàng)建好工程

通過(guò)xib文件來(lái)初始化視圖控制器

二、編寫(xiě)代碼
1、先為NSDictionary創(chuàng)建一個(gè)分類 實(shí)現(xiàn)字典的深拷貝
.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]]; //這里的容量也只是個(gè)參考值,表示對(duì)大小的限制 大小是調(diào)用該方法的count
NSArray *keys = [self allKeys]; //self就是個(gè)可變的字典
for(id key in keys)
{
id dicValue = [self valueForKey:key];
//從 NSDictionary 取值的時(shí)候有兩個(gè)方法objectForkey valueForKey
id dicCopy = nil;
if([dicValue respondsToSelector:@selector(mutableDeepCopy)])
//如果對(duì)象沒(méi)有響應(yīng)mutabledeepcopy 就創(chuàng)建一個(gè)可變副本 dicValue 有沒(méi)有實(shí)現(xiàn)這個(gè)方法
{
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、編寫(xiě)主代碼
.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 //只在第一次加載視圖調(diào)用
{
[super viewDidLoad];
/*加載plist文件*/
NSString *wordsPath = [[NSBundle mainBundle]pathForResource:@"NoteSection" ofType:@"plist"];//得到屬性列表的路徑
NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:wordsPath];
self.allWords = dictionary;
[self resetSearch]; //加載并填充words可變字典和keys數(shù)組
_search.autocapitalizationType = UITextAutocapitalizationTypeNone;//不自動(dòng)大寫(xiě)
_search.autocorrectionType = UITextAutocorrectionTypeNo;//不自動(dòng)糾錯(cuò)
}
//取消搜索或者改變搜索條件
- (void)resetSearch
{
self.words = [self.allWords mutableDeepCopy]; //得到所有字典的副本 得到一個(gè)字典
NSLog(@"所有字典 = %@",self.words);
NSMutableArray *keyArray = [[NSMutableArray alloc]init];//創(chuàng)建一個(gè)可變數(shù)組
[keyArray addObjectsFromArray:[[self.allWords allKeys]sortedArrayUsingSelector:@selector(compare:)]]; //用指定的selector對(duì)array的元素進(jìn)行排序
self.keys = keyArray; //將所有key 存到一個(gè)數(shù)組里面
NSLog(@"所有key = %@",self.keys);
}
//實(shí)現(xiàn)搜索方法
- (void)handleSearchForTerm:(NSString *)searchTerm
{
NSMutableArray *sectionsRemove = [[NSMutableArray alloc]init]; //創(chuàng)建一個(gè)數(shù)組存放我們所找到的空分區(qū)
[self resetSearch];
for(NSString *key in self.keys)//遍歷所有的key
{
NSMutableArray *array = [_words valueForKey:key] ; //得到當(dāng)前鍵key的名稱 數(shù)組
NSMutableArray *toRemove = [[NSMutableArray alloc]init];//需要從words中刪除的值 數(shù)組
for(NSString *word in array) //實(shí)現(xiàn)搜索
{
if([word rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)//搜索時(shí)忽略大小寫(xiě) 把沒(méi)有搜到的值 放到要?jiǎng)h除的對(duì)象數(shù)組中去
[toRemove addObject:word]; //把沒(méi)有搜到的內(nèi)容放到 toRemove中去
}
if([array count] == [toRemove count])//校對(duì)要?jiǎng)h除的名稱數(shù)組長(zhǎng)度和名稱數(shù)組長(zhǎng)度是否相等
[sectionsRemove addObject:key]; //相等 則整個(gè)分區(qū)組為空
[array removeObjectsInArray:toRemove]; //否則 刪除數(shù)組中所有與數(shù)組toRemove包含相同的元素
}
[self.keys removeObjectsInArray:sectionsRemove];// 刪除整個(gè)key 也就是刪除空分區(qū),釋放用來(lái)存儲(chǔ)分區(qū)的數(shù)組,并重新加載table 這樣就實(shí)現(xiàn)了搜索
[_table reloadData];
}
- (void)viewWillAppear:(BOOL)animated //當(dāng)使用Push或者prenset方式調(diào)用
{
}
//#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ([_keys count] >0)?[_keys count]:1; //搜索時(shí)可能會(huì)刪除所有分區(qū) 則要保證要有一個(gè)分區(qū)
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([_keys count] == 0)
{
return 0;
}
NSString *key = [_keys objectAtIndex:section]; //得到第幾組的key
NSArray *wordSection = [_words objectForKey:key]; //得到這個(gè)key里面所有的元素
return [wordSection count]; //返回元素的個(gè)數(shù)
}
- (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]; //得到這個(gè)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;
}
//為每個(gè)分區(qū)指定一個(gè)標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if([_keys count] == 0)
return @" ";
NSString *key = [_keys objectAtIndex:section];
return key;
}
//創(chuàng)建一個(gè)索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _keys;
}
#pragma mark -
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_search resignFirstResponder]; //點(diǎn)擊任意 cell都會(huì)取消鍵盤(pán)
return indexPath;
}
#pragma mark-
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar //搜索button點(diǎn)擊事件
{
NSString *searchTerm = [searchBar text];
[self handleSearchForTerm:searchTerm]; //搜索內(nèi)容 刪除words里面的空分區(qū)和不匹配內(nèi)容
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{ //搜索內(nèi)容隨著輸入及時(shí)地顯示出來(lái)
if([searchText length] == 0)
{
[self resetSearch];
[_table reloadData];
return;
}
else
[self handleSearchForTerm:searchText];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar //點(diǎn)擊取消按鈕
{
_search.text = @""; //標(biāo)題 為空
[self resetSearch]; //重新 加載分類數(shù)據(jù)
[_table reloadData];
[searchBar resignFirstResponder]; //退出鍵盤(pán)
}
@end
運(yùn)行結(jié)果


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS監(jiān)聽(tīng)手機(jī)鎖屏狀態(tài)
iPhone的鎖屏監(jiān)測(cè)分為兩種方式監(jiān)聽(tīng),本文給大家介紹的非常詳細(xì),具體內(nèi)容詳情大家通過(guò)本文詳細(xì)了解下吧2017-05-05
iOS開(kāi)發(fā)xconfig和script腳本使用詳解
這篇文章主要為大家介紹了iOS開(kāi)發(fā)xconfig和script腳本使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
iOS仿小紅書(shū)呼吸燈動(dòng)畫(huà)(核心動(dòng)畫(huà)和定時(shí)器)兩種方式實(shí)現(xiàn)
本篇文章主要介紹了iOS仿小紅書(shū)呼吸燈動(dòng)畫(huà)(核心動(dòng)畫(huà)和定時(shí)器)兩種方式實(shí)現(xiàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04
iOS 仿百度外賣-首頁(yè)重力感應(yīng)的實(shí)例
這篇文章主要介紹了iOS 仿百度外賣-首頁(yè)重力感應(yīng)的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
iOS實(shí)現(xiàn)點(diǎn)擊狀態(tài)欄自動(dòng)回到頂部效果詳解
在IOS開(kāi)發(fā)過(guò)程中,經(jīng)常會(huì)有這種需求,需要通過(guò)點(diǎn)擊狀態(tài)欄返回到頂部,給用戶更好的體驗(yàn)效果,下面這篇文章給大家詳細(xì)介紹了實(shí)現(xiàn)過(guò)程,有需要的可以參考借鑒。2016-09-09
iOS?Swift?Lazy?var?View失效問(wèn)題解決
這篇文章主要為大家介紹了iOS?Swift?Lazy?var?View失效問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

