我有一个cities
数组,我希望用户添加。
在add screen
我希望用户开始输入时,城市(来自数组)的选项将显示在表格视图中。
我在互联网上搜索了几个小时,但一切似乎都很旧而且不是最新的(苹果公司在iOS 8和9中弃用了一些东西)。
我正在寻找有关如何操作的最新教程,任何人都可以提供帮助吗?
谢谢!
答案 0 :(得分:7)
如果我可以澄清......,你想搜索/过滤城市阵列吗?
无论哪种方式,这都是我在iOS9中使用UISearchController进行的方式。
0)你需要这些协议(如果它是一个tableView):
@interface YourViewController () <UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>
1)声明这些属性:
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, readonly) NSArray *searchResults;
3)在viewWillAppear和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)searchController的Getter / Setter:
#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)更新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)更新UISearchControllerDelegate
#pragma mark - UISearchControllerDelegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.tableView reloadData];
}
无论如何,这是它的主旨。祝好运。还有很多关于此的教程。