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

相关文章

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

从基础到高级详解Python数值格式化输出的完全指南

《从基础到高级详解Python数值格式化输出的完全指南》在数据分析、金融计算和科学报告领域,数值格式化是提升可读性和专业性的关键技术,本文将深入解析Python中数值格式化输出的相关方法,感兴趣的小伙... 目录引言:数值格式化的核心价值一、基础格式化方法1.1 三种核心格式化方式对比1.2 基础格式化示例

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?