2014-09-13 23 views
25

Jak poprawić następujące elementy, aby nie wyświetlały się żadne ostrzeżenia? Czego mi brakuje?searchDisplayController przestarzałe w systemie iOS 8

Kiedy sprostowanie searchResultsController do searchController daje mi błąd „nie znaleziono obiektu”

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

    return cell; 
} 

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

i co to może być kolega? patrz edytowany post powyżej, proszę ... –

+1

Nie ma ani jednego. Możesz dodać własny ivar lub własność, aby śledzić go w razie potrzeby. – rmaddy

+4

żartujesz, prawda? dlaczego miałbym to zrobić, aby wykonać tak łatwe zadanie, które zawsze było bezpłatne? co myśli Apple? –

Odpowiedz

12

Klasa UISearchController zastępuje klasę UISearchDisplayController do zarządzania wyświetlaniem interfejsów związanych z wyszukiwaniem.

Źródło: https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

Tak, jak rmaddy powiedział, jeśli chcesz pozbyć się nieaktualnych ostrzeżeń, zaprzestać korzystania z nieaktualnych klas. Użyj UISearchController zamiast:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/cl/UISearchController

+9

Byłoby pomocne wtedy, gdyby Apple usunął przestarzałe klasy z Konstruktora interfejsu. –

4

szukałem do migracji z UISearchDisplayController do UISearchController. Więc znalazłem to na GitHub: https://github.com/dempseyatgithub/Sample-UISearchController
Pobierz/sklonuj to i będziesz mógł zobaczyć, jak używać UISearchController z UITableView i UICollectionView.
Ma wszystko, czego potrzeba do aktualizacji z UISearchDisplayController do UISearchController.
Bardzo pomocna jest także dokumentacja . Dla zaczynasz, spojrzeć na Sample- UISearchController/MasterViewController_TableResults.m i Sample-UISearchController/MasterViewController_FilterResults.m
Jeśli trzeba także wspierać iOS 7 (coś ja osobiście polecam, jeśli jesteś naprawdę zamiar wdrożyć aplikację do App Store) to zrobić:

if([UISearchController class]){ 
//Create an UISearchController and add it to your UITableViewController 
}else{ 
//Create an UISearchDisplayController and add it to your UITableViewController 
} 

Uwaga: Musisz zrobić wszystko programowo jeśli chcesz obsługiwać obie wersje iOS.

0

Od dawna pracujemy nad poprawnym obracaniem nowego kontrolera UISearchController.

Oto jak to wyglądało przed:

Po wielu czasie zasadniczo „zrezygnował” na uczynienie pracy rotacji prawidłowo. Zamiast tego po prostu ukrywamy kontroler wyszukiwania przed rozpoczęciem rotacji. Następnie użytkownik musi nacisnąć przycisk wyszukiwania w obróconym widoku, aby ponownie wyświetlić pasek wyszukiwania.

Oto odpowiedni kod:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { 
    [self.searchController dismissViewControllerAnimated:YES completion:nil]; 
    self.searchControllerWasActive = NO; 
    self.searchButton.enabled = YES; 
} 

Ważna informacja: Nasz kod wykorzystuje UIViewController i nie UITableViewController. Ekran wymaga dodatkowych przycisków, dlatego nie możemy używać UITableViewController. Użycie UISearchController na UITableViewController nie wykazuje problemów z rotacją.

Uważamy, że jest to niezbędna praca, biorąc pod uwagę obecny stan UISearchController. Znacznie lepiej byłoby znaleźć prawdziwe rozwiązanie tego problemu.

8

Dodaj to do swojej.h plik

<UISearchBarDelegate,UISearchResultsUpdating> 

NSArray *searchResultsArray; 
NSMutableArray *userMutableArray; 

@property (retain, nonatomic) UISearchController *searchController; 

i to do pliku .m

userMutableArray = [[NSMutableArray alloc] initWithObjects:@"Jack",@"Julie", nil]; 
searchResultsArray = [[NSArray alloc]init]; 

self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; 
self.searchController.searchBar.scopeButtonTitles = [[NSArray alloc]initWithObjects:@"UserId", @"JobTitleName", nil]; 
self.searchController.searchBar.delegate = self; 
self.searchController.searchResultsUpdater = self; 
[self.searchController.searchBar sizeToFit]; 
self.searchController.dimsBackgroundDuringPresentation = NO; 
self.definesPresentationContext = YES; 
self.tableView.tableHeaderView = self.searchController.searchBar; 


-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{ 
    NSString *searchString = self.searchController.searchBar.text; 
    NSPredicate *resultPredicate; 
    NSInteger scope = self.searchController.searchBar.selectedScopeButtonIndex; 
    if(scope == 0){ 
     resultPredicate = [NSPredicate predicateWithFormat:@"userId contains[c] %@",searchString]; 
    }else{ 
     resultPredicate = [NSPredicate predicateWithFormat:@"jobTitleName contains[c] %@",searchString]; 
    } 
    searchResultsArray = [userMutableArray filteredArrayUsingPredicate:resultPredicate]; 
    [self.tableView reloadData]; 
} 

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{ 
    [self updateSearchResultsForSearchController:self.searchController]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if(self.searchController.active){ 
     return [searchResultsArray count]; 
    }else{ 
     return [userMutableArray count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if(!cell){ 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
    if(self.searchController.active){ 
     cell.textLabel.text = [searchResultsArray objectAtIndex:indexPath.row]; 
    }else{ 
     cell.textLabel.text = [userMutableArray objectAtIndex:indexPath.row]; 
    } 
    return cell; 
} 
+1

Ta odpowiedź działa świetnie. –

+0

To jest najlepsza odpowiedź na oryginalne pytanie - pokazuje praktyczny i działający przykład konwersji z używania przestarzałego UISearchDisplayController na UISearchController. – mbbeme

+0

odpowiedź prosta i szczegółowa. –

Powiązane problemy