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

iOS tableview實(shí)現(xiàn)簡單搜索功能

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

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

一、先用xcode創(chuàng)建好工程

通過xib文件來初始化視圖控制器

二、編寫代碼

1、先為NSDictionary創(chuàng)建一個分類 實(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]]; //這里的容量也只是個參考值,表示對大小的限制 大小是調(diào)用該方法的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)]) 
  //如果對象沒有響應(yīng)mutabledeepcopy 就創(chuàng)建一個可變副本 dicValue 有沒有實(shí)現(xiàn)這個方法
  {
   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 //只在第一次加載視圖調(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;//不自動大寫
 _search.autocorrectionType = UITextAutocorrectionTypeNo;//不自動糾錯
}

//取消搜索或者改變搜索條件
- (void)resetSearch
{
 self.words = [self.allWords mutableDeepCopy]; //得到所有字典的副本 得到一個字典
 NSLog(@"所有字典 = %@",self.words);
 NSMutableArray *keyArray = [[NSMutableArray alloc]init];//創(chuàng)建一個可變數(shù)組
 [keyArray addObjectsFromArray:[[self.allWords allKeys]sortedArrayUsingSelector:@selector(compare:)]]; //用指定的selector對array的元素進(jìn)行排序
 self.keys = keyArray; //將所有key 存到一個數(shù)組里面
 NSLog(@"所有key = %@",self.keys);
}
//實(shí)現(xiàn)搜索方法
- (void)handleSearchForTerm:(NSString *)searchTerm
{
 NSMutableArray *sectionsRemove = [[NSMutableArray alloc]init]; //創(chuàng)建一個數(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ù)組中去
    [toRemove addObject:word]; //把沒有搜到的內(nèi)容放到 toRemove中去
  }
  
  if([array count] == [toRemove count])//校對要刪除的名稱數(shù)組長度和名稱數(shù)組長度是否相等
   [sectionsRemove addObject:key]; //相等 則整個分區(qū)組為空
  [array removeObjectsInArray:toRemove]; //否則 刪除數(shù)組中所有與數(shù)組toRemove包含相同的元素
 }
 [self.keys removeObjectsInArray:sectionsRemove];// 刪除整個key 也就是刪除空分區(qū),釋放用來存儲分區(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; //搜索時可能會刪除所有分區(qū) 則要保證要有一個分區(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]; //得到這個key里面所有的元素
 return [wordSection count]; //返回元素的個數(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]; //得到這個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;
}
//為每個分區(qū)指定一個標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
 if([_keys count] == 0)
  return @" ";
 NSString *key = [_keys objectAtIndex:section];
 return key;
}
//創(chuàng)建一個索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
 return _keys;
}
#pragma mark - 

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 [_search resignFirstResponder]; //點(diǎn)擊任意 cell都會取消鍵盤
 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)容隨著輸入及時地顯示出來
 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]; //退出鍵盤

}
@end

運(yùn)行結(jié)果

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

相關(guān)文章

最新評論