iOS之UISearchDisplayController的使用

本文介绍了一个iOS应用中实现搜索功能的具体方法。通过定义FKViewController类并实现UITableViewDataSource等协议,展示了如何根据用户输入动态更新UITableView的内容。此外,还介绍了如何使用NSPredicate进行字符串匹配来过滤显示的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、.h

#import <UIKit/UIKit.h>

@interface FKViewController : UIViewController<UITableViewDataSource,
	UITableViewDelegate , UISearchBarDelegate , UISearchDisplayDelegate>

@end

2、.m

#import <QuartzCore/QuartzCore.h>
#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController
// 定义一个NSArray保存表格显示的原始数据
NSArray* tableData;
// 定义一个NSArray保存查询结果数据
NSArray* searchData;
bool isSearch;
- (void)viewDidLoad
{
    [super viewDidLoad];
	isSearch = NO;
	// 初始化表格原始显示的数据
	tableData = [NSArray arrayWithObjects:@"疯狂Java讲义",
		@"轻量级Java EE企业应用实战",
		@"疯狂Android讲义",
		@"疯狂Ajax讲义",
		@"疯狂HTML5/CSS3/JavaScript讲义",
		@"疯狂iOS讲义",
		@"疯狂XML讲义",
		@"经典Java EE企业应用实战"
		@"Java入门与精通",
		@"Java基础教程",
		@"学习Java",
		@"Objective-C基础" ,
		@"Ruby入门与精通",
		@"iOS开发教程" , nil];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
	// 如果处于搜索状态
	if(isSearch)
	{
		// 使用searchData作为表格显示的数据
		return searchData.count;
	}
	else
	{
		// 否则使用原始的tableData座位表格显示的数据
		return tableData.count;
	}
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString* cellId = @"cellId";
	// 从可重用的表格行队列中获取表格行
	UITableViewCell* cell = [tableView
		dequeueReusableCellWithIdentifier:cellId];
	// 如果表格行为nil
	if(!cell)
	{
		// 创建表格行
		cell = [[UITableViewCell alloc] initWithStyle:
				UITableViewCellStyleDefault
									  reuseIdentifier:cellId];
	}
	// 将单元格的边框设置为圆角
	cell.layer.cornerRadius = 12;
	cell.layer.masksToBounds = YES;
	// 获取当前正在处理的表格行的行号
	NSInteger rowNo = indexPath.row;
	// 如果处于搜索状态
	if(isSearch)
	{
		// 使用searchData作为表格显示的数据
		cell.textLabel.text = [searchData objectAtIndex:rowNo];
	}
	else{
		// 否则使用原始的tableData座位表格显示的数据
		cell.textLabel.text = [tableData objectAtIndex:rowNo];
	}
	return cell;
}
// UISearchBarDelegate定义的方法,用户单击取消按钮时激发该方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
	// 取消搜索状态
	isSearch = NO;
}
// UISearchBarDelegate定义的方法,当搜索文本框内文本改变时激发该方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
	// 调用filterBySubstring:方法执行搜索
	[self filterBySubstring:searchText];
}
// UISearchBarDelegate定义的方法,用户单击虚拟键盘上Search按键时激发该方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
	// 调用filterBySubstring:方法执行搜索
	[self filterBySubstring:searchBar.text];
	// 放弃作为第一个响应者,关闭键盘
	[searchBar resignFirstResponder];
}
- (void) filterBySubstring:(NSString*) subStr
{
	// 设置为开始搜索
	isSearch = YES;
	// 定义搜索谓词
	NSPredicate* pred = [NSPredicate predicateWithFormat:
		@"SELF CONTAINS[c] %@" , subStr];
	// 使用谓词过滤NSArray
	searchData = [tableData filteredArrayUsingPredicate:pred];
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值