Xor*_*oso 5 objective-c uiview uisearchbar uisearchdisplaycontroller ios
我正在寻找一种方法,如何在点击按钮后显示带有搜索栏的搜索视图。我是否想创建一个带有搜索栏的新 UIView,然后在点击按钮后转到该 UIView?或者有更好的选择吗?
您可以将其想象为在 Youtube 应用程序中搜索,但您会在键入时看到搜索结果。
编辑: 所以现在我设法在点击按钮后显示搜索栏,但它没有正确显示。问题是当搜索栏向下移动时,它被从下切下来:



我的代码:
implementation ViewController {
NSInteger listNo;
DataClass *dataClass;
UITableViewController *searchResultsController;
}
- (void)viewDidLoad {
[super viewDidLoad];
dataClass = [DataClass getInstance];
self.navigationController.navigationBarHidden = YES;
// Prepare Search Controller and Result Controller
searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y + 5, self.searchController.searchBar.frame.size.width, 44.0);
[self.searchController.searchBar sizeToFit];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = @"Ahoj";
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
- (void)didDismissSearchController:(UISearchController *)searchController {
}
// Load data to next view
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showTable"]) {
AdditivesTableViewController *atvc = (AdditivesTableViewController *)segue.destinationViewController;
switch (listNo) {
case 0:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.nonDangerousAdditives];
break;
case 1:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.mediumDangerousAdditives];
break;
case 2:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.dangerousAdditives];
break;
case 3:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.highDangerousAdditives];
break;
default:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.allAdditives];
break;
}
}
}
#pragma mark - IBActions
- (IBAction)nonDangList:(id)sender {
listNo = 0;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)medDangList:(id)sender {
listNo = 1;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)dangList:(id)sender {
listNo = 2;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)highDangList:(id)sender {
listNo = 3;
[self performSegueWithIdentifier:@"showTable" sender:self];
}
- (IBAction)searchAdditive:(id)sender {
[self.view addSubview:self.searchController.searchBar];
[self.searchController setActive:YES];
[self.searchController.searchBar becomeFirstResponder];
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |