如何使用目标c实现UISearchController

时间:2015-08-23 18:05:42

标签: ios objective-c uisearchcontroller

我有一个用objective-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 app tableview中实现新UISearchController的任何指针,示例代码或教程。

1 个答案:

答案 0 :(得分:22)

按照上述步骤进行初始化。

1).h接口类 <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>中的协议声明

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)使用Button甚至启动控制器并通过这些功能以备将来使用(如果有的话)

#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类中声明filterContacts属性,该属性将填充搜索的项目。

8)多数民众赞成在选择行比较表视图(如果是主控制器或自定义控制器类表视图)并对所选项执行操作。

希望这有用。