Ios 搜索显示控制器的问题
我正在学习在iOS中使用搜索栏和搜索显示控制器。我制作了一个包含两个视图的简单测试项目,一个视图包含一个UITableView和一个UISearchBar,当您从表视图中选择一行时,新视图显示一个带有行名称的标签。我遇到了几个问题,首先,当我开始在搜索栏中添加文本,搜索显示出现时,搜索栏消失了。过滤后的结果会显示出来,但当我选择一行时,它不会将我带到下一个视图,这只会在筛选表时发生。代码如下: ViewController.hIos 搜索显示控制器的问题,ios,searchdisplaycontroller,Ios,Searchdisplaycontroller,我正在学习在iOS中使用搜索栏和搜索显示控制器。我制作了一个包含两个视图的简单测试项目,一个视图包含一个UITableView和一个UISearchBar,当您从表视图中选择一行时,新视图显示一个带有行名称的标签。我遇到了几个问题,首先,当我开始在搜索栏中添加文本,搜索显示出现时,搜索栏消失了。过滤后的结果会显示出来,但当我选择一行时,它不会将我带到下一个视图,这只会在筛选表时发生。代码如下: ViewController.h #import <UIKit/UIKit.h> #imp
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
谢谢你的帮助
当我选择一行时,它不会将我带到下一个视图,只有在筛选表时才会发生这种情况
因为您没有实现tableView:didselectrowatinexpath:
记住,实际上没有“过滤表视图”这样的东西。有两个表视图:正常的表视图和由于搜索显示控制器而显示的表视图。这是一个不同的表视图(基本上显示在您的前面),您必须根据您希望它执行的操作和外观对其进行配置。add delegate method didSelectRowAtIndexPath,检查表视图是否是searchDisplayController的表视图,然后调用方法[self-PerformsgueWithIdentifier:sender:。问题可能是您在故事板中有选择序列,但当您在searchDisplayController中单击tableview时,viewcontroller将其作为tableviews序列而不是序列。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSArray *objects;
@property (nonatomic, strong) NSArray *filteredObjects;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_objects = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", @"Ten", nil];
_tableView.delegate = self;
_tableView.dataSource = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [_filteredObjects count];
} else {
return [_objects count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [_filteredObjects objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [_objects objectAtIndex:indexPath.row];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ToDetail"]) {
DetailViewController *detailViewController = [segue destinationViewController];
if (self.searchDisplayController.active) {
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
detailViewController.detailString = [_filteredObjects objectAtIndex:indexPath.row];
} else {
NSIndexPath *indexPath = [_tableView indexPathForSelectedRow];
detailViewController.detailString = [_objects objectAtIndex:indexPath.row];
}
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
_filteredObjects = [_objects filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}