Ios 搜索时将结果加载到表中
我不知道我如何才能做到这一点,我只是在学习表格。我有下面的代码,可以在键入时过滤数组。搜索栏向上移动,搜索下方的控制器变暗,但是当我键入第一个字母时,下面不会生成结果 这是对Apple示例项目的修改。表搜索 我错过了什么 此外,当新的VC加载时,键盘似乎会被拖到后面 先谢谢你Ios 搜索时将结果加载到表中,ios,uitableview,Ios,Uitableview,我不知道我如何才能做到这一点,我只是在学习表格。我有下面的代码,可以在键入时过滤数组。搜索栏向上移动,搜索下方的控制器变暗,但是当我键入第一个字母时,下面不会生成结果 这是对Apple示例项目的修改。表搜索 我错过了什么 此外,当新的VC加载时,键盘似乎会被拖到后面 先谢谢你 // itemsSearchViewController.m #import "itemsSearchViewController.h" #import "SearchRecipeViewController.h"
// itemsSearchViewController.m
#import "itemsSearchViewController.h"
#import "SearchRecipeViewController.h"
#import "firstAppDelegate.h"
@interface itemsSearchViewController ()
@end
@implementation itemsSearchViewController
@synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive;
@synthesize delegate;
@synthesize itemsToPass;
/*
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark -
#pragma mark Lifecycle methods
*/
- (void)viewDidLoad
{
//[super viewDidLoad];
self.title = @"items Search";
//write names of itemss to array
NSString *path =[[NSBundle mainBundle] pathForResource:@"possibleitemss" ofType:@"plist"];
NSArray *content = [NSArray arrayWithContentsOfFile:path];
self.listContent =[NSArray arrayWithArray:content];
if([content count] == 0)
{
NSLog(@"nsma is empty");
}
NSLog(@"list contents%@", listContent);
// NSLog(@"list content = %@", listContent);
// create a filtered list that will contain products for the search results table.
self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];
// restore search settings if they were saved in didReceiveMemoryWarning.
if (self.savedSearchTerm)
{
[self.searchDisplayController setActive:self.searchWasActive];
[self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
[self.searchDisplayController.searchBar setText:savedSearchTerm];
self.savedSearchTerm = nil;
}
[self.tableView reloadData];
self.tableView.scrollEnabled = YES;
}
- (void)viewDidUnload
{
//self.filteredListContent = nil;
}
- (void)viewDidDisappear:(BOOL)animated
{
// save the state of the search UI so that it can be restored if the view is re-created
self.searchWasActive = [self.searchDisplayController isActive];
self.savedSearchTerm = [self.searchDisplayController.searchBar text];
self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
}
- (void)dealloc
{
[listContent release];
[filteredListContent release];
[super dealloc];
}
#pragma mark -
#pragma mark UITableView data source and delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
/*
If the requesting table view is the search display controller's table view, return the count of
the filtered list, otherwise return the count of the main list.
*/
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.filteredListContent count];
}
else
{
return [self.listContent count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
}
/*
If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
*/
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
If the requesting table view is the search display controller's table view, configure the next view controller using the filtered content, otherwise use the main list.
*/
}
#pragma mark -
#pragma mark Content Filtering
//as user types this is happening
-(void) filterResults:(NSString *)searchText{
NSMutableArray *test = [NSMutableArray arrayWithArray:self.listContent];
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (int i=0; i<[test count]; i++) {
NSString *stringResult = [test objectAtIndex:i];
NSComparisonResult result = [stringResult compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[self.filteredListContent addObject:stringResult];
}
}
[self.filteredListContent sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];//sort alphabetically
NSLog(@"filtered results = %@",self.filteredListContent);
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{items
[self filterResults:searchString];
//[self filterContentForSearchText:searchString scope:
// [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (IBAction)itemsSelected: (id)sender{
if ( self.delegate != nil && [self.delegate respondsToSelector:@selector(showSearchRecipeView)] ) {
[self.delegate showSearchRecipeView];
}
}
@end
//itemsSearchViewController.m
#导入“itemsSearchViewController.h”
#导入“SearchRecipeViewController.h”
#导入“firstAppDelegate.h”
@接口项SearchViewController()
@结束
@实现项SearchViewController
@合成listContent、filteredListContent、savedSearchTerm、savedScopeButtonIndex、searchWasActive;
@综合代表;
@综合项目总类;
/*
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
#布拉格标记-
#pragma标记生命周期方法
*/
-(无效)viewDidLoad
{
//[超级视图下载];
self.title=@“项目搜索”;
//将项的名称写入数组
NSString*path=[[NSBundle mainBundle]pathForResource:@“plist”类型的@“possibleItems”;
NSArray*content=[NSArray arrayWithContentsOfFile:path];
self.listContent=[NSArray arrayWithArray:content];
如果([内容计数]==0)
{
NSLog(@“nsma为空”);
}
NSLog(@“列表内容%@”,列表内容);
//NSLog(@“列表内容=%@”,列表内容);
//创建包含搜索结果表产品的筛选列表。
self.filteredListContent=[NSMutableArray阵列容量:[self.listContent计数]];
//如果搜索设置保存在didReceiveMemoryWarning中,则恢复搜索设置。
if(self.savedSearchTerm)
{
[self.searchDisplayController设置活动:self.searchwas活动];
[self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
[self.searchDisplayController.searchBar设置文本:savedSearchTerm];
self.savedSearchTerm=nil;
}
[self.tableView重载数据];
self.tableView.scrollEnabled=是;
}
-(无效)视图卸载
{
//self.filteredListContent=nil;
}
-(无效)视图消失:(BOOL)已设置动画
{
//保存搜索UI的状态,以便在重新创建视图时可以恢复该状态
self.searchWasActive=[self.searchDisplayController isActive];
self.savedSearchTerm=[self.searchDisplayController.searchBar text];
self.savedScopeButtonIndex=[self.searchDisplayController.searchBar selectedScopeButtonIndex];
}
-(无效)解除锁定
{
[列表内容发布];
[过滤列表内容发布];
[super dealoc];
}
#布拉格标记-
#pragma标记UITableView数据源和委托方法
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
/*
如果请求的表视图是搜索显示控制器的表视图,则返回
过滤列表,否则返回主列表的计数。
*/
if(tableView==self.searchDisplayController.searchResultsTableView)
{
返回[self.filteredListContent count];
}
其他的
{
返回[self.listContent count];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*kCellID=@“cellID”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:kCellID];
如果(单元格==nil)
{
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:kCellID]自动释放];
}
/*
如果请求的表视图是search display controller的表视图,请使用筛选的内容配置单元格,否则使用主列表。
*/
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
/*
如果请求的表视图是搜索显示控制器的表视图,请使用筛选的内容配置下一个视图控制器,否则使用主列表。
*/
}
#布拉格标记-
#pragma标记内容过滤
//随着用户类型的增加,这种情况正在发生
-(void)filterResults:(NSString*)搜索文本{
NSMutableArray*test=[NSMutableArray arrayWithArray:self.listContent];
[self.filteredListContent removeAllObjects];//首先清除已筛选的数组。
对于(int i=0;i我猜下面的一个是错误的:
- 资源路径是
possibleItems
——这是打字错误吗
- 您是否已验证阵列是否已填充
- 您的
searchDisplayController:shouldReloadTableForSearchScope:
是否实际返回yes
换句话说,我将进入这些函数中的每一个,观察流的行为是否符合您的预期,以及您的数组是否已填充
如果有帮助,请告诉我