微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 如何用客观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];

这就是我所拥有的!

我会感激任何指针,示例代码或教程如何在现有的目标c应用程序表视图中实现新的UISearchController.

解决方法

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

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

8)这样做,在选择行中比较表视图,如果它的主控制器或自定义控制器类表视图,并对所选项进行操作.

希望这是有帮助的.

原文地址:https://www.jb51.cc/iOS/330477.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐