-3
我想添加一个搜索栏到我存在的UIViewController类。我使用iOS 8+,并且总是调试我的应用程序,我将收到一条错误消息。添加搜索栏到UIViewController类
我所做的:
.H
@interface mainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate>
.M
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResultArray count];
}
else{
return [self.fullArray count];
}
}
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchResultArray];
searchResultArray = [self.fullArray filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResultArray objectAtIndex:indexPath.row];
}
else{
cell.textLabel.text = [self.fullArray objectAtIndex:indexPath.row];
}
}
但代码会给我一个警告:
searchDisplayController is deprecated: first deprecated in iOS 8.0
我该如何解决这个问题?
查看“UISearchDisplayController”的文档。它告诉你现在要做什么。 – rmaddy
我发现这个:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchDisplayController_Class/,但它不是真的有用... – hantoren
它怎么没有帮助?您是否至少阅读过这些文档的第一段? – rmaddy