2015-08-23 55 views
13

我有一個現有的應用程序,用Objective-C編寫,帶有一個表視圖。如何實現具有目標的UISearchController c

我現在試圖回到這個應用程序,並添加一個搜索欄到表中。

問題是,現在有新的UISearchController協議,似乎很少有信息在線如何在objective-c中實現這一點 - 我可以找到的所有教程和示例都適用於Swift。

我已經添加了代表們的.h文件中:

UISearchBarDelegate, UISearchResultsUpdating 

而且我在viewDidLoad下面的代碼,它的工作原理,並增加了一個搜索欄:

// Search controller 
searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
searchController.searchResultsUpdater = self; 
searchController.dimsBackgroundDuringPresentation = NO; 
searchController.searchBar.delegate = self; 

// Add the search bar 
self.tableView.tableHeaderView = searchController.searchBar; 
self.definesPresentationContext = YES; 
[searchController.searchBar sizeToFit]; 

這是因爲就我所知!

我將不勝感激任何指針,示例代碼或教程如何在現有的Objective-C應用程序tableview中實現新的UISearchController。

+1

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html – soulshined

+0

我發現下面的示例代碼,這是迄今爲止我發現的最有用的示例:https://github上。com/Ja5onHoffman/UISearchController-Demo – Richard

回答

19

初始化按照上述步驟執行以下操作。

1)協議聲明在<UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>在.H接口類

2)聲明以下性質

//Fetch result controller 
@property (nonatomic, strong) UISearchController *searchController; 

//for the results to be shown with two table delegates 
@property (nonatomic, strong) CLCustomerResultrowsItemsCellController *searchResultsController; 

//this custom controller is only suppose to have number of rows and cell for row function of table datasource 

3)對於狀態恢復

@property BOOL searchControllerWasActive; 
@property BOOL searchControllerSearchFieldWasFirstResponder; 

4)在這一步中viewDidLoad中

_searchResultsController = [[CLChatContactsSearchResultController alloc] init]; 
_searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResultsController]; 

self.searchController.searchResultsUpdater = self; 
self.searchController.searchBar.placeholder = nil; 
[self.searchController.searchBar sizeToFit]; 
self.contactsTableView.tableHeaderView = self.searchController.searchBar; 


// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables 
self.searchResultsController.tableView.delegate = self; 
self.searchController.delegate = self; 
self.searchController.dimsBackgroundDuringPresentation = YES; // default is YES 
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others 

// Search is now just presenting a view controller. As such, normal view controller 
// presentation semantics apply. Namely that presentation will walk up the view controller 
// hierarchy until it finds the root view controller or one that defines a presentation context. 
// 
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed 

5初始化代碼)使用按鈕甚至啓動控制器和過去的這些功能以供將來使用,如果任何看評論

#pragma mark - UISearchBarDelegate 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [searchBar resignFirstResponder]; 
} 


#pragma mark - UISearchControllerDelegate 

// Called after the search controller's search bar has agreed to begin editing or when 
// 'active' is set to YES. 
// If you choose not to present the controller yourself or do not implement this method, 
// a default presentation is performed on your behalf. 
// 
// Implement this method if the default presentation is not adequate for your purposes. 
// 
- (void)presentSearchController:(UISearchController *)searchController { 

} 

- (void)willPresentSearchController:(UISearchController *)searchController { 
    // do something before the search controller is presented 
} 

- (void)didPresentSearchController:(UISearchController *)searchController { 
    // do something after the search controller is presented 
} 

- (void)willDismissSearchController:(UISearchController *)searchController { 
    // do something before the search controller is dismissed 
} 

- (void)didDismissSearchController:(UISearchController *)searchController { 
    // do something after the search controller is dismissed 
} 

6 )在文本搜索中,您可以獲得此回調

#pragma mark - UISearchResultsUpdating 

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController { 

    // update the filtered array based on the search text 
    NSString *searchText = searchController.searchBar.text; 

    id <NSFetchedResultsSectionInfo> sectionInfo = [_fetchedResultsController.sections objectAtIndex:0]; 

    if (searchText == nil) { 

     // If empty the search results are the same as the original data 
     self.searchResults = [sectionInfo.objects mutableCopy]; 

    } else { 

     NSMutableArray *searchResults = [[NSMutableArray alloc] init]; 

     NSArray *allObjects = sectionInfo.objects; 

     for (PhoneNumber *phoneMO in allObjects) { 

      if ([phoneMO.number containsString:searchText] || [[phoneMO.closrr_id filteredId] containsString:searchText] || [[phoneMO.contact.fullname lowercaseString] containsString:[searchText lowercaseString]]) { 
       [searchResults addObject:phoneMO]; 
      } 
     } 

     self.searchResults = searchResults; 

    } 

    // hand over the filtered results to our search results table 
    CLCustomerResultrowsItemsCellController *tableController = (CLCustomerResultrowsItemsCellController *)self.searchController.searchResultsController; 
    tableController.filteredContacts = self.searchResults; 
    [tableController.tableView reloadData]; 
} 

7)您必須在Custom類中聲明filteredContacts屬性,以填充搜索到的項目。

8)這就是說,它在選擇行比較表視圖,如果它的主控制器或自定義控制器類表視圖,並執行所選項目的操作。

希望這是有幫助的。

+1

你能告訴,什麼是'CLCustomerResultrowsItemsCellController'?它給我錯誤,不能導入。 –

+1

呵呵CLCustomerResultrowsItemsCellController不過是一個tableviewcontroller類,它有兩個方法和一個屬性filteredresults nsmutablearray。提到在reload和cellforrowatindexpath上找到的項目的行數,通過使用confirmgurecell來創建一個單元格:...用uisearchcontroller檢查IOS TableViewcontroller的例子,它更容易理解。 –

+0

如果你喜歡答案,並解決你的目的,請接受答案,並打勾:) –

相關問題