0
votes

I want to add uisearchbar in table view controller, but I search the uisearchbar tutorial in iOS 9 and try it. I can’t show the uisearchbar on the simulator of my project. It also has not the search function. If I pass the code “self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(self.searchController.searchBar.frame));”, the Xcode will show the error message. This message is “libc++abi.dylib: terminating with uncaught exception of type NSException”.

Could tell me where is wrong?

Platform: iPhone in iOS 10.3 and iPad in iOS 10.3 Xcode version:8.3.3 (8E3004b)

.h file

#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 files

#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.

1

1 Answers

0
votes

I have found out the answer. I neglect the updateSearchResultsForSearchController code.

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.tableView reloadData];
}