2014-12-03 2 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

проверили ли вы свой фильтр? Контент для поискового запроса получает вызов или нет? –

+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]; 

connect bar поиска delegate от storyboard.