在iOS10中如何使用objective c中的uisearchbar?
How to use the uisearchbar in objective c in iOS 10?
我想在table视图控制器中添加uisearchbar,但是我在iOS9中搜索了uisearchbar教程并尝试了。我无法在我的项目模拟器上显示 uisearchbar。它也没有搜索功能。如果我传递代码“self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(self.searchController.searchBar.frame));”,Xcode 将显示错误消息。此消息是“libc++abi.dylib:终止并出现 NSException 类型的未捕获异常”。
能告诉我哪里错了吗?
平台:iPhone 在 iOS 10.3 和 iPad 在 iOS 10.3
Xcode version: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;
}
Interface image 1 about outlet setting.
UISearchbar outlet setting.
Search Display Controller outlet setting.
我找到答案了。
我忽略了 updateSearchResultsForSearchController 代码。
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.tableView reloadData];
}
我想在table视图控制器中添加uisearchbar,但是我在iOS9中搜索了uisearchbar教程并尝试了。我无法在我的项目模拟器上显示 uisearchbar。它也没有搜索功能。如果我传递代码“self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(self.searchController.searchBar.frame));”,Xcode 将显示错误消息。此消息是“libc++abi.dylib:终止并出现 NSException 类型的未捕获异常”。
能告诉我哪里错了吗?
平台:iPhone 在 iOS 10.3 和 iPad 在 iOS 10.3 Xcode version: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;
}
Interface image 1 about outlet setting.
UISearchbar outlet setting.
Search Display Controller outlet setting.
我找到答案了。 我忽略了 updateSearchResultsForSearchController 代码。
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.tableView reloadData];
}