【发布时间】:2017-12-06 11:02:18
【问题描述】:
我想实现类似 gmail 应用程序的搜索控制器,
或者我想像这个例子那样实现
图片 1 它显示带有搜索控制器按钮的普通收件箱
图片2点击搜索按钮后会展开显示如图2。在这里我们可以输入单词...等
图 3 无论我们输入什么,取决于它在列表中显示的文本,当我们单击它时,它将导航到详细视图。
我想实现同样的事情。
唯一的一点是,当我开始在搜索控制器中输入时,一个 API 会调用,即搜索,然后根据搜索中输入的文本,它将作为参数并在列表(表格视图)中显示数据。此数据将来自 API 响应。
当我点击任何结果时,它必须移动详细视图。
任何人都可以帮助我。
我是 iOS 的新手。刚刚开始学习iOS。请帮我解决这个问题。
这是.h文件
#import <UIKit/UIKit.h>
@interface PDSearchExampleTableViewController : UITableViewController
{
BOOL searching;
}
@property (strong, nonatomic) UISearchBar *searchBar;
@property (strong, nonatomic) NSMutableArray *sampleDataArray;
@property (strong, nonatomic) NSMutableArray *filteredSampleDataArray;
- (IBAction)searchButtonClicked:(id)sender;
@end
这是.m文件
#import "PDSearchExampleTableViewController.h"
@interface PDSearchExampleTableViewController () <UISearchDisplayDelegate, UISearchBarDelegate>
@end
@implementation PDSearchExampleTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_searchBar.delegate = self;
_sampleDataArray = [[NSMutableArray alloc] init];
_filteredSampleDataArray = [[NSMutableArray alloc] init];
[_sampleDataArray addObject:@"one"];
[_sampleDataArray addObject:@"two"];
[_sampleDataArray addObject:@"three"];
[_sampleDataArray addObject:@"four"];
[_sampleDataArray addObject:@"five"];
[_sampleDataArray addObject:@"six"];
[_sampleDataArray addObject:@"seven"];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (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 (searching) {
return [_filteredSampleDataArray count];
} else {
return [_sampleDataArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sampleSearchCell" forIndexPath:indexPath];
if (searching) {
cell.textLabel.text = [_filteredSampleDataArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [_sampleDataArray objectAtIndex:indexPath.row];
}
return cell;
}
- (IBAction)searchButtonClicked:(id)sender {
self.navigationItem.rightBarButtonItem = nil;
_searchBar = [[UISearchBar alloc] init];
_searchBar.delegate = self;
_searchBar.placeholder = @"Search Sample Data";
[_searchBar sizeToFit];
self.navigationItem.titleView = _searchBar;
[_searchBar becomeFirstResponder];
[_searchBar.window makeKeyAndVisible];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[_searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searching = NO;
[self.tableView reloadData];
self.navigationItem.titleView = nil;
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchButtonClicked:)];
[_searchBar setShowsCancelButton:NO];
[_searchBar resignFirstResponder];
self.navigationItem.rightBarButtonItem = rightBarButton;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[_filteredSampleDataArray removeAllObjects];
if ([searchText length] != 0) {
searching = YES;
[self searchData];
} else {
searching = NO;
}
[self.tableView reloadData];
}
- (void)searchData {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", _searchBar.text];
NSArray *tempArray = [_sampleDataArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@", tempArray);
_filteredSampleDataArray = [NSMutableArray arrayWithArray:tempArray];
}
【问题讨论】:
-
请发表你到目前为止做了什么......!
-
我有一个 tableview,比如 gmail 收件箱。我从 json 获取数据,并以 tableview 格式显示。
-
我的意思是说你在视图控制器中做了什么发布部分代码
-
我将分享代码。我现在想要的是,当我开始输入搜索控制器时,我想知道如何在运行时调用 API。
-
这里是 api -> api/v1/inbox/ticket-search?
标签: ios objective-c search uisearchbar uisearchcontroller