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

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

zoj3820(树的直径的应用)

题意:在一颗树上找两个点,使得所有点到选择与其更近的一个点的距离的最大值最小。 思路:如果是选择一个点的话,那么点就是直径的中点。现在考虑两个点的情况,先求树的直径,再把直径最中间的边去掉,再求剩下的两个子树中直径的中点。 代码如下: #include <stdio.h>#include <string.h>#include <algorithm>#include <map>#

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

【区块链 + 人才服务】可信教育区块链治理系统 | FISCO BCOS应用案例

伴随着区块链技术的不断完善,其在教育信息化中的应用也在持续发展。利用区块链数据共识、不可篡改的特性, 将与教育相关的数据要素在区块链上进行存证确权,在确保数据可信的前提下,促进教育的公平、透明、开放,为教育教学质量提升赋能,实现教育数据的安全共享、高等教育体系的智慧治理。 可信教育区块链治理系统的顶层治理架构由教育部、高校、企业、学生等多方角色共同参与建设、维护,支撑教育资源共享、教学质量评估、

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定