IOS5基础之十三-----实现搜索栏

2023-10-12 18:50
文章标签 基础 实现 搜索 十三 ios5

本文主要是介绍IOS5基础之十三-----实现搜索栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

为什么要把搜索栏单独写,主要是这里牵涉到一个深层可变副本。在这里为什么要用这个~~~~你迷茫吗?

我也很迷茫哈哈~~~~~~

该项目是请一个项目的加强版,虽然只是多了一个搜索控件,可是却多了许多步骤。

上次公司需要添加一个字段。也就是在数据库中新增一个字段。我排的时间相对长了一点,受到众人笑话。可是当你对系统的复杂性了解后,你会知道特别时在数据库增加字段时带来了很多的问题。并且我这个字段几乎整个项目的所有表都要增加。因为每张表的关联性,并且有些地方要添加,有的地方使用存储过程,有的是创建临时表,如果要时间短,是可以完成,但是谁有知道那些地方没有修改,当过了一段时间后,就会有一段垃圾数据出现,那时也只有去修改数据库了,哈哈废话一段!!!!也许还是我的能力有限。

前面我使用了多数组的字段,其中字母表中的每个字母都占用一个数组。该字典是不可改变的。这就意味着不能从字典添加和删除值。它包含的数组也是如此。所以要创建两个字典,一个包含完整数据集的不可改变的字典,一个可以从中删除行的可变的字典副本。

复习一下,浅层复制和深层复制

浅层复制:不复制引用对象,新复制的对象值指向现有的引用对象。

深层复制:将复制所用的引用对象。

NSDictionary遵循NSMutableCopying协议,该方法创建的是一个浅层副本。但是引用对象是不能删除的,所以是无法删除对象的。需要一个类别去存放数组的字典的副本。


添加类别后 项目导航变成


在头文件中做一个接口返回NSMutbleDictionary类型的方法

#import <Foundation/Foundation.h>

@interface NSDictionary (MutableDeepCopy)

-(NSMutableDictionary *) mutableDeepCopy;

@end


实现改方法

- (NSMutableDictionary *)mutableDeepCopy {

    NSMutableDictionary *returnDict = [[NSMutableDictionary alloc]

                                       initWithCapacity:[self count]];

    NSArray *keys = [self allKeys];

    for (id key in keys) {

        id oneValue = [self valueForKey:key];

        id oneCopy = nil;

        

        if ([oneValue respondsToSelector:@selector(mutableDeepCopy)])

            oneCopy = [oneValue mutableDeepCopy];

        else if ([oneValue respondsToSelector:@selector(mutableCopy)])

            oneCopy = [oneValue mutableCopy];

        if (oneCopy == nil)

            oneCopy = [oneValue copy];

        [returnDict setValue:oneCopy forKey:key];

    }

    return returnDict;

}

用一个数组存储这个这个字典。遍历并且判断对象如果没有响应mutableDeepCopy消息,那么它将创建可变副本,否则就创建常规副本。

for (id  key in keys)称为快速枚举类似C#中的foreach()方法。NSDictionary 、NSArray、NSSet都支持快速枚举。


现在头文件修改为

#import <UIKit/UIKit.h>


@interface BIDViewController :UIViewController

<UITableViewDataSource,UITableViewDelegate>


@property (strong,nonatomicIBOutlet UITableView *table;   //指向输出口视图

@property (strong,nonatomicIBOutlet UISearchBar *search;  //指向输出口搜索栏

@property (strong,nonatomicNSDictionary *allNames;        //字典存放所有的数据集

@property (strong,nonatomicNSMutableDictionary *names;    //将存有那些与当前搜索条件匹配的数据集

@property (strong,nonatomicNSMutableArray *keys;          //将存有索引值和分区名称

@property (assignnonatomicBOOL isSearching;             //是否在使用搜索栏

-(void)resetSearch;                                        //复制数据

-(void)handleSearchForTerm:(NSString *)searchTerm;          //实现搜索的方法


@end

实现方法如下

#import "BIDViewController.h"

#import "NSDictionary+MutableDeepCopy.h"


@implementation BIDViewController

@synthesize table;

@synthesize search;

@synthesize allNames;

@synthesize names;

@synthesize keys;

@synthesize isSearching;


#pragma mark

#pragma mark Custom Methods

-(void)resetSearch

{

    self.names =[self.allNames mutableDeepCopy];

    NSMutableArray *keyArray= [[NSMutableArray allocinit];

    [keyArray addObject:UITableViewIndexSearch];

    [keyArray addObjectsFromArray:[[self.allNames allKeyssortedArrayUsingSelector:@selector(compare:)]];

    self.keys=keyArray;

}


-(void) handleSearchForTerm:(NSString *)searchTerm

{

    NSMutableArray *sectionsToRemove=[[NSMutableArray allocinit];//创建一个数组,存放我们找到的空分区。

  

    [self resetSearch];

    for(NSString *key in self.keys)

    {

        NSMutableArray *array=[names valueForKey:key];//存放需要从names数组中删除的值的数组

        NSMutableArray *toRemove=[[NSMutableArray alloc]init];

       

        for(NSString *name in array)

        {

            //循环使用一个字符串中子字符串位置的NSString的方法。并且返回一个NSRange结构,如果返回的包含了NSNotFound就添加到要删除的对象数组中

            if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound

            {

                [toRemove addObject:name];

            }

        }

        if ([array count]==[toRemove count]) {

            [sectionsToRemove addObject:key];

        }

        [array removeObjectsInArray:toRemove];//从此分区中删除不匹配的对称

    }

     //删除空分区 并告知重新加载数据

    [self.keys removeObjectsInArray:sectionsToRemove];

    //[sectionsToRemove release];

    [table reloadData];

}




- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


//初始化数据

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSString *path= [[NSBundle mainBundlepathForResource:@"sortednames"ofType:@"plist"];

    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];

    self.allNames=dict;

    [self resetSearch];

    [table reloadData];

    [table setContentOffset:CGPointMake(0.0,44.0animated:NO];//设置表中内容的偏移量

    

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

    self.names=nil;

    self.keys=nil;

    self.allNames=nil;

    self.table=nil;

    self.search=nil;

}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

}


- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

}


- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

}


- (void)viewDidDisappear:(BOOL)animated

{

    [super viewDidDisappear:animated];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);

}


#pragma mark

#pragma mark Table View Data Source Methods

//指定分区的数量

-(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];

    NSArray *nameSection=[names objectForKey:key];

    return [nameSection count];

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    NSUInteger section=[indexPath section];

    NSUInteger row=[indexPath row];

    

    NSString *key =[keys objectAtIndex:section];

    NSArray *nameSection=[names objectForKey:key];

    

    static NSString *SectionsTableIdentifier=@"SectionsTableIdentifiler";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    if (cell==nil) {

        cell=[[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier];

    }

    cell.textLabel.text=[nameSection objectAtIndex:row];

    return cell;

}


//为每个分区指定一个可选的标题值,然后只返回这一组的字母就可以了

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    if ([keys count]==0) {

        return nil;

    }

    NSString *key=[keys objectAtIndex:section];

    if (key==UITableViewIndexSearch) {

        return nil;

    }

    return  key;

}


//添加索引的方法

-(NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView

{

    if (isSearching)

        return nil;

    return keys;

}


#pragma mark -

#pragma mark Table View Delegate Methods

- (NSIndexPath *)tableView:(UITableView *)tableView

  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [search resignFirstResponder];//如果用户在使用搜索栏时单击一行,我们希望键盘不再起作用。

    isSearching = NO;

    search.text = @"";

    [tableView reloadData];

    return indexPath;

}


#pragma mark -

#pragma mark Search Bar Delegate Methods

//当用户单击键盘上的返回按钮或搜索按钮时,调用,此方法从搜索栏获取搜索短语。并调用搜索方法。

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    NSString *searchTerm = [searchBar text];

    [self handleSearchForTerm:searchTerm];

}


//实时搜索,只要搜索栏中的短语发生变化都重新搜索,这个是需要设备的高性能。

- (void)searchBar:(UISearchBar *)searchBar

    textDidChange:(NSString *)searchTerm {

    if ([searchTerm length] == 0) {

        [self resetSearch];

        [table reloadData];

        return;

    }

    [self handleSearchForTerm:searchTerm];

}


//取消按钮的触发的事件

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    isSearching = NO;

    search.text = @"";

    [self resetSearch];

    [table reloadData];

    [searchBar resignFirstResponder];

}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

    isSearching = YES;

    [table reloadData];

}


- (NSInteger)tableView:(UITableView *)tableView

sectionForSectionIndexTitle:(NSString *)title

               atIndex:(NSInteger)index {

    NSString *key = [keys objectAtIndex:index];

    if (key == UITableViewIndexSearch) {

        [tableView setContentOffset:CGPointZero animated:NO];

        return NSNotFound;

    } else return index;

}


@end


提到一个搜索的放大器功能。

3个步骤:

a。向keys数组添加一个特殊值以指示我们需要一个放大镜。

-(void)resetSearch

{

    self.names =[self.allNames mutableDeepCopy];

    NSMutableArray *keyArray= [[NSMutableArray allocinit];

    [keyArray addObject:UITableViewIndexSearch];

    [keyArray addObjectsFromArray:[[self.allNames allKeyssortedArrayUsingSelector:@selector(compare:)]];

    self.keys=keyArray;

}


b。必须阻止IOS在表格中显示该特殊值的部分标题。

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    if ([keys count]==0) {

        return nil;

    }

    NSString *key=[keys objectAtIndex:section];

    if (key==UITableViewIndexSearch) {

        return nil;

    }

    return  key;

}

c。告诉表格在该项被选中时滚至顶部。

- (NSInteger)tableView:(UITableView *)tableView

sectionForSectionIndexTitle:(NSString *)title

               atIndex:(NSInteger)index {

    NSString *key = [keys objectAtIndex:index];

    if (key == UITableViewIndexSearch) {

        [tableView setContentOffset:CGPointZero animated:NO];

        return NSNotFound;

    } else return index;

}


遇到很多问题。

1。在视图页面的时候,没有在UIView中添加一个View,直接将Search Bar 拖进去。无法修改其长度。

2。 self.names =[self.allNames mutableDeepCopy];的时候mutableCopy,报数组不能变成可变的。

3。说error code。反复验证对比没有发现错误,后来网上一查,说要重启电脑错误自动消失,抓狂,搞的我花费了很长的事件认真核对代码。

其实代码是出来了,还是有很多地方不是很理解。要抄出来还是挺费劲的,估计是自己比较弱,哈哈。虽然花了2~3天的时间。这里还是要重复多看看。

这篇关于IOS5基础之十三-----实现搜索栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/197778

相关文章

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Pydantic中model_validator的实现

《Pydantic中model_validator的实现》本文主要介绍了Pydantic中model_validator的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录引言基础知识创建 Pydantic 模型使用 model_validator 装饰器高级用法mo

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进