在 Apple 开发者论坛的tartempion的帮助下,我终于在一个文件中管理了一个 UTableviewController 和 searchViewController,使我能够使用相同的故事板场景来显示搜索结果。
@interface TableViewController () <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
{
UISearchController *_searchcontroller;
NSArray * myArray;
NSArray * myDisplayedArray;
}
@end
@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[...]
_searchcontroller=[[UISearchController alloc]initWithSearchResultsController:nil];
_searchcontroller.searchResultsUpdater =self;
_searchcontroller.dimsBackgroundDuringPresentation=NO;
myDisplayedArray=myArray;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myDisplayedArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyClass*myInstance =[myDisplayedArray objectAtIndex:[indexPath row]];
//and however you want to configure cells...
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//whatever you need...
//and to get rid of the searchcontroller when you move to the UIView that is connected
self.searchcontroller.active=NO;
//or
[self.searchcontroller dismissViewControllerAnimated:NO completion:nil];
}
#pragma mark -
- (void)updateSearchResultsForSearchController:(UISearchController *)inSearchController
{
if (_searchController==inSearchController)
{
NSString *tSearchText = inSearchController.searchBar.text;
if ([tSearchText length]==0)
{
myDisplayedArray=myArray;
}
else
{
myDisplayedArray=[myArray filteredWithPattern:tSearchText];
}
[self.tableView reloadData];
}
}
@end