0
所以我在我的項目中使用UISearchController,它似乎工作一切正常,但無論我在搜索欄上輸入什麼,我不會得到任何結果。所以我相信問題出在我的 (void)updateSearchResultsForSearchController:(UISearchController *)searchController { }。說實話,我真的不知道該用哪種方法編碼。這裏是代碼的其餘部分:UISearchController結果沒有被過濾
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) SearchResultsTableViewController *resultsTableController;
@property (nonatomic, strong) NSMutableArray *searchResults; // Filtered search results
// for state restoration
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;
_resultsTableController = [[SearchResultsTableViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.tintColor = [UIColor darkGrayColor];
self.definesPresentationContext = YES;
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// restore the searchController's active state
if (self.searchControllerWasActive) {
self.searchController.active = self.searchControllerWasActive;
_searchControllerWasActive = NO;
if (self.searchControllerSearchFieldWasFirstResponder) {
[self.searchController.searchBar becomeFirstResponder];
_searchControllerSearchFieldWasFirstResponder = NO;
}
}}
#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];}
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
嘿拉里我不知道你的意思是用戶在條件? – 2014-12-04 05:04:25
NSString * searchText = searchController.searchBar.text; NSMutableArray * searchResults = [self.tableData mutableCopy]; if([searchText length] == 0){self.searchResults addObjectsFromArray:self.tableData]; (NSString * searchString in self.tableData){ NSString * name = [searchString self];其他{ }其他{ NSRange range = [[name lowercaseString] rangeOfString:[searchText lowercaseString]]; if(range.location!= NSNotFound) [self.searchResults addObject:searchString];}} [self.tableView reloadData]; – 2014-12-04 05:08:30
對不起塞爾吉奧。我應該解釋說,我的初始數據來自用戶的NSArray(self.users here),每個用戶類都是一個User類(基本上是一個NSDictionary)。我遍歷每個用戶,將他們的全名與搜索文本進行比較。如果找到,我將該條目添加到self.filtered客戶端NSArray中,該客戶端用於填充我的表格視圖。然後我重新加載表格視圖。你會注意到,如果沒有搜索文本,我所有的用戶都會使用self.filteredclient,以便表視圖顯示它們全部。 – 2014-12-04 13:09:00