#import <UIKit/UIKit.h>
@interface VersioniViewController_TableResults : UITableViewController
@property (nonatomic, strong) NSArray *versioni;
@end
#import "VersioniViewController_TableResults.h"
#import "VersioniDetailViewController.h"
#import "SearchVersioniViewController.h"
#import "Versioni.h"
#define ENABLE_SCOPE_BUTTONS 1
@interface VersioniViewController_TableResults () <UISearchResultsUpdating, UISearchBarDelegate>
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResultsVersioni; //Filtered search results
@end
#pragma mark -
@implementation VersioniViewController_TableResults
- (void)viewDidLoad {
[super viewDidLoad];
self.versioni = [Versioni allVersioni];
// Create a mutable array to contain products for the search results table.
self.searchResultsVersioni = [NSMutableArray arrayWithCapacity:[self.versioni count]];
// The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"versioniController"];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
#if ENABLE_SCOPE_BUTTONS
NSMutableArray *scopeButtonTitles = [[NSMutableArray alloc] init];
[scopeButtonTitles addObject:NSLocalizedString(@"All", @"Search display controller All button.")];
for (NSString *deviceType in [Versioni deviceTypeNames]) {
NSString *displayName = [Versioni displayNameForType:deviceType];
[scopeButtonTitles addObject:displayName];
}
self.searchController.searchBar.scopeButtonTitles = scopeButtonTitles;
self.searchController.searchBar.delegate = self;
#endif
self.definesPresentationContext = YES;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"pushDetailVersioni"]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Versioni *versioni = self.versioni[indexPath.row];
VersioniDetailViewController *destinationController = segue.destinationViewController;
destinationController.versioni = versioni;
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.versioni count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ProductCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Versioni *versioni = [self.versioni objectAtIndex:indexPath.row];
cell.textLabel.text = versioni.name;
return cell;
}
#pragma mark - UISearchResultsUpdating
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
NSString *scope = nil;
NSInteger selectedScopeButtonIndex = [self.searchController.searchBar selectedScopeButtonIndex];
if (selectedScopeButtonIndex > 0) {
scope = [[Versioni deviceTypeNames] objectAtIndex:(selectedScopeButtonIndex - 1)];
}
[self updateFilteredContentForProductName:searchString type:scope];
if (self.searchController.searchResultsController) {
UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;
SearchVersioniViewController *vdvc = (SearchVersioniViewController *)navController.topViewController;
vdvc.searchResultsVersioni = self.searchResultsVersioni;
[vdvc.tableView reloadData];
}
}
#pragma mark - UISearchBarDelegate
// Workaround for bug: -updateSearchResultsForSearchController: is not called when scope buttons change
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
[self updateSearchResultsForSearchController:self.searchController];
}
#pragma mark - Content Filtering
- (void)updateFilteredContentForProductName:(NSString *)productName type:(NSString *)typeName {
if ((productName == nil) || [productName length] == 0) {
// If there is no search string and the scope is "All".
if (typeName == nil) {
self.searchResultsVersioni = [self.versioni mutableCopy];
} else {
// If there is no search string and the scope is chosen.
NSMutableArray *searchResults = [[NSMutableArray alloc] init];
for (Versioni *versioni in self.versioni) {
if ([versioni.type isEqualToString:typeName]) {
[searchResults addObject:versioni];
}
}
self.searchResultsVersioni = searchResults;
}
return;
}
[self.searchResultsVersioni removeAllObjects]; // First clear the filtered array.
/* Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
for (Versioni *versioni in self.versioni) {
if ((typeName == nil) || [versioni.type isEqualToString:typeName]) {
NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
NSRange productNameRange = NSMakeRange(0, versioni.name.length);
NSRange foundRange = [versioni.name rangeOfString:productName options:searchOptions range:productNameRange];
if (foundRange.length > 0) {
[self.searchResultsVersioni addObject:versioni];
}
}
}
}
@end
我的第一个功能正常,但是为了更新卡,我得到api密钥没有设置错误