012 在Xcode4.5上创建IOS6.0应用 (高级控件 表视图 搜索框)

2023-12-10 02:38

本文主要是介绍012 在Xcode4.5上创建IOS6.0应用 (高级控件 表视图 搜索框),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

IOS中的高级控件表视图(为表视图加入搜索框)


在上面的一篇博客中我们已经实现的表视图控件
011 在Xcode4.5上创建IOS6.0应用 (高级控件 表视图 分段表视图)

在前面的代码中,或者说基础上要相实现搜索框其实也非常简单,只要再加入一个控件


搜索框有两种,选择的时候我们要尽量选择下面一种因为下面那一种已经实现了该控件的一些方法

再加入SearchBar的协议,实现其的两个方法
协议:

UISearchBarDelegate

方法:

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText


就可以简单的实现一个搜索框的效果
下面就贴出详细的代码


ViewController.h
@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>{NSMutableDictionary *allteams;NSMutableDictionary *teams;NSArray *teamsname;
}@property (nonatomic,retain)NSMutableDictionary *allteams;
@property (nonatomic,retain)NSMutableDictionary *teams;
@property (nonatomic,retain)NSArray *teamsname;-(void)resetSearch;@end

ViewController.m
@implementation ViewController@synthesize allteams;
@synthesize teams;
@synthesize teamsname;//重新搜索
-(void)resetSearch{self.teams = self.allteams;NSMutableArray *keyArray = [[NSMutableArray alloc] init];[keyArray addObjectsFromArray:[[teams allKeys] sortedArrayUsingSelector:@selector(compare:)] ];self.teamsname = keyArray;[keyArray release];
}//加载数据
- (void)viewDidLoad
{[super viewDidLoad];//下面为模式代码读取文件到代码中NSBundle *bundle = [NSBundle mainBundle];NSString *filePath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];self.allteams = dic;[dic release];[self resetSearch];
}//加载数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//返回段里面有几行NSString *name = [teamsname objectAtIndex:section];NSArray *team = [teams objectForKey:name];return [team count];
}
//返回数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return [teamsname count];
}
//返回每个段里面的名字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{NSString *name = [teamsname objectAtIndex:section];return name;
}
//模式代码填充数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}NSUInteger section = [indexPath section];NSUInteger row = [indexPath row];NSString *name = [teamsname objectAtIndex:section];NSArray*team = [teams objectForKey:name];//返回协议的标题cell.textLabel.text = [team objectAtIndex:row];return cell;
}
//实现索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{return teamsname;
}
//实现表示图的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSUInteger section = [indexPath section];NSUInteger row = [indexPath row];NSString *name = [teamsname objectAtIndex:section];NSArray *team = [teams objectForKey:name];NSString *selectedteam = [team objectAtIndex:row];NSString *message = [[NSString alloc] initWithFormat:@"你选择的号码是%@",selectedteam];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"球队选择"message:message delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];[alert show];[alert release];[message release];//实现点击时,让点击的那个选中慢慢消失[tableView deselectRowAtIndexPath:indexPath animated:YES];}//查询的方法
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{[self resetSearch];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{if([searchText length] == 0){[self resetSearch];return;}NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];for(NSString *key in self.allteams){NSMutableArray *arry = [allteams valueForKey:key];NSMutableArray *newTeams = [[NSMutableArray alloc]init];for(NSString *teamName in arry){if([teamName rangeOfString:searchText options:NSCaseInsensitiveSearch].location !=NSNotFound){[newTeams addObject:teamName];}}if([newTeams count] > 0){[dict setObject:newTeams forKey:key];}[newTeams release];}self.teamsname = [[dict allKeys]sortedArrayUsingSelector:@selector(compare:)];self.teams = dict;[dict release];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[allteams release];[teams release];[teamsname release];[super dealloc];
}@end

最后看看效果图

这篇关于012 在Xcode4.5上创建IOS6.0应用 (高级控件 表视图 搜索框)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien