2014-12-04 6 views
0

내 프로젝트에서 UISearchController를 사용하고 있으며 검색 표시 줄에 아무리 입력해도 결과가 없습니다. 따라서 문제가 발생한 것 같습니다. (void) updateSearchResultsForSearchController : (UISearchController *) searchController { }. 솔직히 말해서, 나는 그 코드에 어떤 코드를 넣을 지 정말로 모른다. 당신은, 당신의 테이블 뷰 데이터가 나오는있는 NSArray을 당신의 검색 기준에 따라 그것을 필터링 한 다음 테이블 뷰를 다시로드해야합니다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 { 
} 

답변

0

: 여기에 코드의 나머지 부분입니다. 귀하의 코드는 다음과 같이 보일 수 있습니다 :

- (void) doSearch:(NSString *)searchText { 
    [self.filteredClients removeAllObjects]; 

    if ([searchText length] == 0) { 
     [self.filteredClients addObjectsFromArray:self.users]; 
    } else { 
     for (User *user in self.users) { 
      NSString *name = [user fullName]; 
      NSRange range = [[name lowercaseString] rangeOfString:[searchText lowercaseString]]; 
      if (range.location != NSNotFound) 
       [self.filteredClients addObject:user]; 
     } 
    } 
    [self.tableView reloadData]; 
} 


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    if ([searchText length] > 0) 
    { 
     [self showCancelButton:YES]; 
    } 
    else { 
     [self showCancelButton:NO]; 
     [searchBar resignFirstResponder]; 
    } 
    [self doSearch:searchText]; 
} 


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    [self showCancelButton:YES]; 
} 

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    if ([searchBar.text length] > 0) [self showCancelButton:YES]; 
    else        [self showCancelButton:NO]; 
    [self doSearch:_searchBar.text]; 
} 
+0

안녕하세요 래리 나는 for의 조건에서 사용자가 무슨 뜻인지 확신하지 못합니까 ?? –

+0

NSString * searchText = searchController.searchBar.text; NSMutableArray * searchResults = [self.tableData mutableCopy]; if ([searchText length] == 0) { [self.searchResults addObjectsFromArray : self.tableData]; else { for (NSString * searchString in self.tableData) { NSString * name = [searchString self]; NSRange range = [[이름 lowercaseString] rangeOfString : [검색 텍스트 lowercaseString]]; if (range.location! = NSNotFound) [self.searchResults addObject : searchString];}} [self.tableView reloadData]; –

+0

미안 Sergio. 나는 나의 초기 데이터가 각각 NSArray (기본적으로 NSDictionary) 인 사용자의 NSArray (여기서는 self.users)에서 온 것이라고 설명 했어야한다. 각 사용자에 대해 반복하고 전체 이름과 검색 텍스트를 비교합니다. 발견되면, 필자는 테이블보기를 채우는 데 사용되는 self.filtered 클라이언트 NSArray에 해당 항목을 추가합니다. 그런 다음 표보기를 다시로드합니다. 검색 텍스트가 없으면 모든 사용자가 self.filteredclients를 참조하므로 테이블보기에 모두 표시됩니다. –

관련 문제