使用iOS 9上的搜索栏

FS.*_*S.O -1 iphone cocoa-touch objective-c uisearchbar ios

我有一个cities数组,我希望用户添加.

add screen我想,当用户开始打字,为城市的选项(从阵列)将显示在一个表视图.

我在互联网上搜索了几个小时,但一切似乎都很旧而且不是最新的(苹果在iOS 8和9中弃用了一些东西).

我正在寻找有关如何做到这一点的最新教程,任何人都可以提供帮助吗?

谢谢!

sal*_*lyp 7

如果我可以澄清......,你想搜索/过滤城市阵列吗?

无论哪种方式,这都是我在iOS9中使用UISearchController进行的方式.

0)您需要这些协议(如果它是一个tableView):

@interface YourViewController () <UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>
Run Code Online (Sandbox Code Playgroud)

1)声明这些属性:

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, readonly) NSArray *searchResults;
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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 @[];
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)

5)更新UISearchControllerDelegate

#pragma mark - UISearchControllerDelegate

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    [self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

无论如何,这是它的主旨.祝好运.这里有很多关于这方面的教程.