该程序实现了1到10000之间的数字搜索
1,声明UISearchBar和UITableView的委托
@interface ViewController : UIViewController <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
2,声明全局变量
@interface ViewController () { NSMutableArray *dataArr; NSMutableArray *filterDataArr; UITableView *numberTableView; UISearchBar *numberSearchBar; }
dataArr : 存储所有要搜索的数据,1-10000
filterDataArr : 存储搜索到的数据
3,初始化数据,将1-10000存储到dataArr和filterDataArr中
filterDataArr = [[NSMutableArray alloc] init]; dataArr = [[NSMutableArray alloc] init]; for (int i = 0; i < 10000; i++) { [dataArr addObject:[[NSString alloc] initWithFormat:@"%d", i]]; [filterDataArr addObject:dataArr[i]]; }
4,初始化UITableView
numberTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, numberSearchBar.frame.origin.y + numberSearchBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 80)]; numberTableView.delegate = self; numberTableView.dataSource = self; [self.view addSubview:numberTableView];
5,初始化UISearchBar
numberSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(40, 40, self.view.frame.size.width - 80, 40)]; numberSearchBar.delegate = self; numberSearchBar.barStyle = UIBarStyleBlack; //黑色风格 默认是白色 numberSearchBar.barTintColor = [UIColor whiteColor]; //搜索框外背景变为百色 [self.view addSubview:numberSearchBar];
6,实现UISearchBar委托
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { [filterDataArr removeAllObjects]; 过去数据全部消除 for (int i = 0; i < dataArr.count; i++) { if ([dataArr[i] containsString:numberSearchBar.text]) { [filterDataArr addObject:dataArr[i]]; } } [numberTableView reloadData]; //重置TableView }
7,实现UITableViewDelegate和UITableViewDataSource
//返回tableView的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return filterDataArr.count; }
//搜索的数字显示在tableView每一行 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } else{ // 删除cell中的子对象,解决覆盖问题。 while ([cell.contentView.subviews lastObject] != nil) { [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; } } if (filterDataArr.count == 0) { cell.textLabel.text = @""; } else { cell.textLabel.text = filterDataArr[indexPath.row]; } return cell; }
//每行高度为42 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 42; }
//tableView的点击效果 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
效果如图
相关推荐
iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView...
使用UITableView实现的可折叠分组下拉列表组件
TreeView for iPhone,《使用UITableView实现树视图》一文源代码
实现简单的UITableView对图片的显示。可以展示多张图片。
在iOS中用UIbutton和UItableview实现下拉列表。仅供参考,如果有不足之处,期待各位的分享~
ios UISearchDisplayController 实现 UITableView 搜索功能 - LeslieFang - 博客园1
UITableView实现时间轴效果
实现 app 中常见的搜索框(UISearchBar)在列表视图中的滚动行为。本代码实现了三种情况,一种是搜索框始终位于列表的顶部,列表的拖动时搜索框都固定不动。第二种是,列表视图刚加载时,搜索框隐藏在视图的顶部,当...
ios-swift-使用表格组件(UITableView)实现分组列表.zip
使用ASI开源库,实现队列下载。使用UITableView进行展示
表格视图实现滚动视差效果,博客地址:http://blog.csdn.net/hierarch_lee/article/details/51335157,欢迎点击阅读。
UITableView实现格瓦拉飞天投票效果
一个tableView页面,左上角的按钮用来添加行,同时可以移动行,右上角的按钮用来删除行,不能移动行,要求第一行不能被改变(删除,移动,增加),第一行用来返回上一级。 参考博客:...
使用Masonry结合UITableView-FDTemplateLayoutCell,纯代码实现聊天文字消息界面的展示;
NULL 博文链接:https://andli.iteye.com/blog/1608267
IOS 本地图片 uitableview 瀑布流 沙盒功能。功能和结构都清晰
UITableView是iPhone中比较常用的,用的比较多的控件, 本例中说明iPhone UITableView的使用方法实例。 该实列中是手动增加UITableViewDataSource和UITableViewDelegate协议来实现的。
ASIHTTPRequest+UITableView实现多个下载任务,没用到重用机制,还有没有实现断点续载,很简单的一个demo,相信初学者都能看懂,还写了一些注释。
UITableView、UITableView基本用法、UITableView详解,UITabelView常用方法
UICollectionView和UITableView切换