我想在表视图控制器中添加uisearchbar,但是我在iOS 9中搜索uisearchbar教程并进行尝试。我无法在项目的模拟器上显示uisearchbar。它还没有搜索功能。如果我通过代码“ self.tableView.contentOffset = CGPointMake(0,CGRectGetHeight(self.searchController.searchBar.frame));”,则Xcode将显示错误消息。此消息是“ libc ++ abi.dylib:以类型为NSException的未捕获异常终止”。
能告诉我哪里错了吗?
平台:iOS 10.3中的iPhone和iOS 10.3中的iPad Xcode版本:8.3.3(8E3004b)
.h文件
#import <UIKit/UIKit.h>
@interface SearchBarTableViewController : UITableViewController <UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>
@property (nonatomic, strong) UISearchController *searchController;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property(nonatomic,strong) NSMutableArray *RowArray;
@end
.m个文件
#pragma mark Search bar Part
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!_searchController.searchBar.superview) {
self.tableView.tableHeaderView = self.searchController.searchBar;
}
if (!self.searchController.active && self.searchController.searchBar.text.length == 0) {
self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(self.searchController.searchBar.frame));
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.searchController.isActive) {
self.searchController.active = NO;
}
}
#pragma mark - Getters / Setters
- (UISearchController *)searchController {
if (!_searchController) {
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.delegate = self;
[_searchController.searchBar sizeToFit];
}
return _searchController;
}
- (NSArray *)searchResults {
NSString *searchString = self.searchController.searchBar.text;
if (searchString.length > 0) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
return [self.RowArray filteredArrayUsingPredicate:predicate];
}
else {
return self.RowArray;
}
return @[];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.searchController.isActive && self.searchController.searchBar.text.length > 0) {
return self.searchResults.count;
}
else {
return self.RowArray.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
NSString *cityName = @"";
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (self.searchController.isActive && (![self.searchController.searchBar.text isEqualToString:@""])) {
cityName = [self.searchResults objectAtIndex:indexPath.row];
}
else {
cityName = [self.RowArray objectAtIndex:indexPath.row];
}
cell.textLabel.text = cityName;
return cell;
}
答案 0 :(得分:0)
我已经找到答案了。 我忽略了updateSearchResultsForSearchController代码。
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.tableView reloadData];
}