2011-08-30 12 views

Odpowiedz

13

Sam to rozwiązałem.

Anuluj Przycisk>

(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    [controller.searchBar setShowsCancelButton:YES animated:NO]; 
    for (UIView *subview in [controller.searchBar subviews]) { 
     if ([subview isKindOfClass:[UIButton class]]) { 
      [(UIButton *)subview setTitle:@"_____" forState:UIControlStateNormal]; 
     } 
    } 
} 

Brak wyników Tekst>

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView { 
    if (!isChangedNoResults) { 
     if ([contactManager.filteredPeople count] == 0) { 
      [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeNoResultsTextToKorean:) userInfo:nil repeats:YES]; 
     } 
    } 
} 

używam wartość timera i bool. Jeśli nie ma zegara, nie można zmienić tekstu, gdy "Brak wyników" pokazuje się jako pierwszy.

- (void)changeNoResultsTextToKorean:(NSTimer *)timer { 
    if (isChangedNoResults) { 
     [timer invalidate]; 
    } 
    else { 
     for (UIView *subview in [self.searchDisplayController.searchResultsTableView subviews]) { 
      if ([subview isKindOfClass:[UILabel class]]) { 
       UILabel *targetLabel = (UILabel *)subview; 
       if ([targetLabel.text isEqualToString:@"No Results"]) { 
        NSLog(@"Changed!"); 
        [targetLabel setText:@"_____"]; 
        isChangedNoResults = YES; 
        [timer invalidate]; 
       } 
      } 
     } 
    } 
} 
+0

czy mimo to można to zrobić bez przeszukiwania subviews w iOS 5.0 i Arc? – inforeqd

+0

Może to nie jest potrzebne w iOS5 + – ChangUZ

+0

Napisałem odpowiedni artykuł na temat tego rodzaju problemów: http://artsy.github.com/blog/2012/05/11/on-making-it-personal--in-iOS -with-searchbars/ – orta

4

Dziękuję ChangUZ za znalezienie sposobu. Teraz, w celu ulepszenia, nie jest potrzebny zegar do zmiany etykiety "Brak wyników".

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    dispatch_async(dispatch_get_main_queue(), ^(void) { 
     for (UIView *v in controller.searchResultsTableView.subviews) { 
      if ([v isKindOfClass:[UILabel self]]) { 
       ((UILabel *)v).text = @"_____"; 
       break; 
      } 
     } 
    }); 
    return YES; 
} 
5

W celu zmiany „nie wynik” tekst można użyć:

[self.searchDisplayController setValue:@"my no result text" forKey: @"noResultsMessage"]; 

Właśnie testowane systemów iOS 8

+0

Testowany w iOS7 i iOS8. Działa jak czar. – tounaobun

+0

działało dobrze w iOS 9.1 – drshock

0

Prostszym rozwiązaniem dla zmiany anuluj tekst:

[self.searchDisplayController.searchBar setValue:@"custom text" forKey:@"cancelButtonText"]; 

Testowane iOS 10

Powiązane problemy