ios - 如何以编程方式创建 UISearchDisplayController?

标签 ios uisearchdisplaycontroller

当我尝试分配 UISearchDisplayController

UISearchBar *searchBar = [[UISearchBar alloc] initiWithFrame: CGRectMake(0,0,310,44)];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController:self];

它给我一个错误:

Assignment to readonly property

当我以编程方式添加 UISearchController 时,我没有得到 tableview of UISearchController :

UITableView *resultsTableView  = [[UITableView alloc] initWithFrame:self.myMapView.frame style:UITableViewStylePlain];
self.searchResultController = [[UITableViewController alloc] init];
self.searchResultController.tableView = resultsTableView;
[self.searchResultController.tableView setDelegate:self];
[self.searchResultController.tableView setDataSource:self];UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 310, 44)];
searchControllerMain = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
searchControllerMain.searchBar.delegate = self;
searchControllerMain.searchResultsUpdater = self;
[searchControllerMain.searchBar sizeToFit];
searchBar.delegate = self;

最佳答案

这在 iOS 8 上使用,searchDisplayController 在 iOS 8 中被弃用。 实现委托(delegate)方法

@interface SearchVC : UITableViewController<UISearchResultsUpdating, UISearchControllerDelegate>

@property (strong, nonatomic) NSMutableArray *arrOptions;
@property (strong, nonatomic) NSArray *searchResult;

@property (strong, nonatomic) UISearchController *searchController;
@property (nonatomic) BOOL isSearchActive;
@property (strong, nonatomic) UITableViewController *seachResultController;


@end

@implementation SearchVC

UITableView *resultsTableView = [[UITableView alloc]initWithFrame:self.tableView.frame];
self.seachResultController=[[UITableViewController alloc]init];
self.seachResultController.tableView=resultsTableView;
[self.seachResultController.tableView setDelegate:self];
[self.seachResultController.tableView setDataSource:self];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdentifier"];
[self.seachResultController.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdentifier"];

self.searchController=[[UISearchController alloc] initWithSearchResultsController:self.seachResultController];
self.searchController.searchResultsUpdater=self;
self.searchController.delegate=self;
[self.searchController.searchBar sizeToFit];

[self.searchController.searchBar setUserInteractionEnabled:NO];

self.tableView.tableHeaderView = self.searchController.searchBar;

self.definesPresentationContext=true;



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.

if (tableView == self.seachResultController.tableView) {
    return self.searchResult.count;
}
else{
    return self.arrOptions.count;
 }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];

if (cell == nil) {
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
}

if (tableView==self.seachResultController.tableView) {
    [cell.textLabel setText:self.searchResult[indexPath.row]];
}
else{
    [cell.textLabel setText:self.arrOptions[indexPath.row]];
}
// Configure the cell...

return cell;
}

关于ios - 如何以编程方式创建 UISearchDisplayController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30208997/

相关文章:

ios - 导致没有 dealloc 被调用的通知

ios - 自动续订内购订阅用户流量&刷新收据

objective-c - 使用 sqlite3_exec 在 Objective-c 中执行事务并没有达到预期效果

ios - UISearchBar 到 UISearchDisplayController 的转换关闭了 20px

uisearchbar - 防止 UISearchDisplayController 隐藏导航栏

ios - UITableView 编辑模式下的页眉和页脚缩进

iOS uisearchviewcontroller 用于 web 服务

ios - 如何更改搜索栏后面的背景颜色

ios - 如何在 UISearchBar 激活时隐藏 UIRefreshControl?

objective-c - 语言速度 c vs objective c