如何以编程方式创建UISearchDisplayController?

时间:2015-05-13 08:01:23

标签: ios uisearchdisplaycontroller

当我尝试分配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;

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

错误在这一行:

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

应该是:

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