我会尽可能详细地说明这一点以获得最佳帮助:
我首先创建了基本选项卡式应用,并在第一个视图控制器中添加了表格视图。我将delegate和dataSource属性设置为第一个视图控制器。我让桌子工作正常,但现在我想要一个搜索栏。我的FirstViewController.h看起来像:
@interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate>
@end
以下是我的FirstViewController.m的大部分内容:
@implementation FirstViewController{
NSDictionary *restaurants;
NSArray *restaurantDisplayNames;
NSArray *searchResults;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return restaurants.count;
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSLog(@"called");
NSString *searchText = searchController.searchBar.text;
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
searchResults = [restaurantDisplayNames filteredArrayUsingPredicate:resultPredicate];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = restaurantDisplayNames[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES
];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle]URLForResource:@"RestaurantList" withExtension:@"plist"];
restaurants = [NSDictionary dictionaryWithContentsOfURL:url];
restaurantDisplayNames = restaurants.allKeys;
// Do any additional setup after loading the view, typically from a nib.
}
问题是,我的搜索栏实际上什么都不做。事实上,即使被调用,我也没有NSLog
- ed的方法。我觉得我接近解决方案,但我现在需要的是:
提前感谢您的帮助!