我按照这里的教程创建了一个搜索栏:http: //www.appcoda.com/how-to-add-search-bar-uitableview/
但是我的搜索结果没有返回任何内容。我能想到的唯一一件事NSArray
是 不是可变的,但是当更改 resultPredicate 给我一个指针类型不兼容的错误时。
。H
@interface plistTableViewController : UITableViewController<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
NSArray *tabledata;
NSArray *searchResults;
}
@end
.m
#import "plistTableViewController.h"
#import "DetailViewController.h"
@interface plistTableViewController ()
@end
@implementation plistTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSString *mylist = [[NSBundle mainBundle] pathForResource:@"lodges" ofType:@"plist"];
tabledata = [[NSArray alloc]initWithContentsOfFile:mylist];
NSLog(@"%@", tabledata);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [tabledata count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellName"];
cell.detailTextLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellSubtitle"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
// search bar code
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText];
searchResults = [tabledata filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"lodgedetail" sender: self];
}
}
// end of searchbar code
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
NSDictionary *contactInfo = tabledata[indexPath.row];
DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
dvc.contactInfo = contactInfo;
if ([segue.identifier isEqualToString:@"lodgedetail"]) {
DetailViewController *dvc = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
dvc.contactInfo = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
dvc.contactInfo = [tabledata objectAtIndex:indexPath.row];
}
}
}
@end