Working with search bar on iOS 9 [closed]

2019-06-14 16:45发布

问题:

I have a cities array that I want the user the add.

On the add screen I want that when the user start typing, the options for the cities (from the array) will be displayed in a table view.

I've searched on the internet for hours but everything seems to be old and not up-to-date (apple deprecated some things in iOS 8 and 9).

I'm searching for an up-to-date tutorial on how to do it, anyone can help?

Thank you!

回答1:

If I may clarify..., do you want to search/filter on the cities array?

Either way, this is the way I do it with UISearchController in iOS9.

0) You need these protocols (if it's a tableView):

@interface YourViewController () <UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>

1) Declare these properties:

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, readonly) NSArray *searchResults;

3) in viewWillAppear and viewWillDisappear

- (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;
    }
}

3) Getter/Setters for the searchController:

#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.cities filteredArrayUsingPredicate:predicate];
    }
    else {
        return self.cities;
    }

    return @[];
}

4) Update tableView:

#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.cities.count;
        }         
  return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    NSString *cityName = @"";
        cell = [tableView dequeueReusableCellWithIdentifier:@"YourIdentifier" forIndexPath:indexPath];
        if (self.searchController.isActive && (![self.searchController.searchBar.text isEqualToString:@""])) {
            cityName = [self.searchResults objectAtIndex:indexPath.row];
        }
        else {
            cityName = [self.cities objectAtIndex:indexPath.row];
        }
        ((YourTableViewCell *)cell).cityNameLabel.text = cityName;
      }
     return cell;
    }

5) Update UISearchControllerDelegate

#pragma mark - UISearchControllerDelegate

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

Anyways, that's the jist of it. Good luck. There's plenty of tutorials on this as well.