【问题标题】:how to create UISearchDisplayController programmatically?如何以编程方式创建 UISearchDisplayController?
【发布时间】:2015-05-13 08:01:22
【问题描述】:

当我尝试分配 UISearchDisplayController

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

它给了我一个错误:

分配给只读属性

当我以编程方式添加 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 uisearchdisplaycontroller


【解决方案1】:

这在 iOS 8 上使用,searchDisplayController 在 iOS 8 中已弃用。 实现委托方法

@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;
}

【讨论】:

  • 当我以编程方式添加 UISearchController 时,我没有得到它的表格视图:
  • 请看编辑答案,这个答案是你的问题吗
  • Nope 仍然不起作用....我的控制器是一个视图控制器,我在导航栏中添加了 searchController 并且我在其中有 mapView...当我搜索我想要放置的东西时地图视图上的表格视图....
【解决方案2】:

错误在这一行:

self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

应该是:

UISearchDisplayController *searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2013-11-15
    • 2013-12-28
    • 2011-02-10
    相关资源
    最近更新 更多