2014-12-03 4 views
0

저는 iOS 개발을 처음하고 검색 결과에 대한 도움을 얻으려고합니다. 여기 UISearchBar의 결과를 표시하는 데 문제가 있습니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    mytableview.delegate = self; 
    mytableview.dataSource = self; 
    SearchBar.delegate = self; 

    titleArray = [[NSMutableArray alloc] initWithObjects:@"Arora town",@"Domino pizza ",@"thai hunt",nil]; 

    subtitleArray = [[NSMutableArray alloc] initWithObjects:@"wX<w (rHR< cg)",@"o;vDRuwkR(vXw>csK;cHed;o;wkRtd.tCd)<*kRuhRto; (vXw>urXur.tylR)<o;vDRxl.",nil]; 
} 

은 cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static NSString *cellidentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 
    } 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     cell.textLabel.text = [titleArray objectAtIndex:indexPath.row]; 
    } 
    return cell; 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText]; 

    self.searchResults = [self.array filteredArrayUsingPredicate:predicate]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
         objectAtIndex:[self.searchDisplayController.searchBar 
              selectedScopeButtonIndex]]]; 

    return YES; 
} 
입니다 .. 코드는 여기에 .. 모든 것이 잘 작동하지만 단어를 검색 할 때 나는 당신의 도움을 주셔서 감사를 통해 수 있도록,이 결과를 표시하지 않습니다
+0

'self.array'는 어디에서 초기화 되었습니까? – Paulw11

+0

filterContentForSearchText가 전화를 받았는지 확인 했습니까? –

+0

이 메소드를 작성한 UISearchBar가 사용되었습니다. - (void) searchBar : (UISearchBar *) theSearchBar textDidChange : (NSString *) searchText –

답변

0
Try This i Have tested. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    if(tableView==self.searchDisplayController.searchResultsTableView) 
     return _results.count; 
    else 
     return _countryNames.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"Cell"; 
    UITableViewCell *cell; 
    cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 
    if(cell==nil) 
    { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 

    } 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     cell.textLabel.text = [_results objectAtIndex:indexPath.row]; 
    } else { 
     cell.textLabel.text = [_countryNames objectAtIndex:indexPath.row]; 
    } 


    return cell; 


} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate 
           predicateWithFormat:@"SELF contains[cd] %@", 
           searchText]; 
    NSLog(@"%@",scope); 
    _results = (NSMutableArray *)[_countryNames filteredArrayUsingPredicate:resultPredicate]; 
} 
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
          scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
            objectAtIndex:[self.searchDisplayController.searchBar 
               selectedScopeButtonIndex]]]; 
    return YES; 
} 

in viewDidLoad 
initialize 
_results=[[NSMutableArray alloc]init]; 
_countryNames=[[NSMutableArray alloc] initWithObjects:@"India",@"Pakistan",@"West Indies",@"Zimbabwe", nil]; 

검색 바 연결 delegatestoryboard.

관련 문제