如何用目标 c 实现 UISearchController

Posted

技术标签:

【中文标题】如何用目标 c 实现 UISearchController【英文标题】:How to implement UISearchController with objective c 【发布时间】:2015-08-23 18:05:41 【问题描述】:

我有一个现有的应用程序,用 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 应用程序表视图中实现新 UISearchController 的指针、示例代码或教程,我将不胜感激。

【问题讨论】:

developer.apple.com/library/ios/samplecode/… 我找到了以下示例代码,这是迄今为止我找到的最有用的示例:github.com/Ja5onHoffman/UISearchController-Demo 【参考方案1】:

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

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

#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) 就是这样,在选择行中比较表视图,如果它是主控制器或自定义控制器类表视图,并为所选项目执行操作。

希望这有帮助。

【讨论】:

你能告诉我,CLCustomerResultrowsItemsCellController 是什么吗?它给了我错误,无法导入。 哦 CLCustomerResultrowsItemsCellController 只是一个 tableviewcontroller 类,它有两个方法和一个属性 filteredresults nsmutablearray。提到在 reload 和 cellforrowatindexpath 上找到的项目的行数,以通过使用配置单元创建单元格:...检查 IOS TableViewcontroller 和 uisearchcontroller 示例,它更容易解决理解。 如果你喜欢这个答案并解决了你的目的,请接受答案并打勾:) 哦,这不是我的问题,我还能接受答案吗?我将以这种方式实现它,如果它有效,我一定会投票赞成。 您是否在使用任何.xib 文件?我认为UISearchController 应该有它的.xib。如果你能添加一些额外的文件会更好,比如这个自定义CLCustomerResultrowsItemsCellController并稍微澄清一下工作流程,更详细地解释它,特别是UISearchController如何与另一个自定义UITableViewController交互,然后,控制器应该有.xib 来自定义实际视图。我们应该将UISearchBarUISearchBar and Search Display Controller 放在.xib 中吗?

以上是关于如何用目标 c 实现 UISearchController的主要内容,如果未能解决你的问题,请参考以下文章

如何用C/C++开发一个web应用(Windows)?

如何用c语言来实现鼠标移动。

Java InputStream函数`available()`如何用C语言实现?

进一步澄清“如何用objective c实现UISearchController”

如何用流明5.8中的dingo修复实现jwt。*?目标[Dingo Api Contract Routing Adapter]不可实例化

如何用Python实现支持向量机