iOS:如何使用目标 c 实现搜索控制器以及如何在运行时调用 API,即当我开始输入搜索控制器时?
Posted
技术标签:
【中文标题】iOS:如何使用目标 c 实现搜索控制器以及如何在运行时调用 API,即当我开始输入搜索控制器时?【英文标题】:iOS: How to implement search controller using objective c & How to call API at runtime i.e when I start typing in search controller? 【发布时间】: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? 【参考方案1】:按照下面的步骤,它肯定会和你的截图不完全一样。
STEP 1:在导航栏上创建一个导航按钮,并添加一个搜索它的图像按钮。这可以通过使用故事板或以编程方式完成。
对该按钮执行操作,
- (IBAction)searchButtonAction:(id)sender
SearchViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"id1"];
[self.navigationController pushViewController:vc animated:YES];
单击此按钮后,它将导航到 SearchViewController 视图。
看故事板,设计如下图
并在 SearchViewController 中编写以下代码,
创建 textField 的 IBOutlet 并对搜索按钮进行操作,并添加 tableView 并为其添加委托和数据源。
@property (weak, nonatomic) IBOutlet UITextField *seachTextField;
@property (weak, nonatomic) IBOutlet UITableView *tableview1;
和
- (BOOL)textFieldShouldReturn:(UITextField *)textField
[_seachTextField resignFirstResponder];
return YES;
// This method asks the delegate whether the specified text should be replaced in the text view.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
NSLog(@"Search String is : %@",_seachTextField.text);
NSString *dataFromSearchTextField=[NSString stringWithFormat:@"%@",_seachTextField.text];
return YES;
- (IBAction)searchButtonAction:(id)sender
// call method for which takes search string
[self ApiMethodCall:_seachTextField.text];
//After clicking on search button, below method is called i.e t API is called
-(void)ApiMethodCall:(NSString *)searchText
// call your API with parameters
// you will get JSON
就是这样,现在轮到你了。从可能包含数组和字典的 JSON 中获取数据。只需做 JSON 解析,编写表格视图数据源和委托方法并显示数据。
【讨论】:
简单但不错。我实施了同样的事情。对图片中显示的建议有任何想法吗?【参考方案2】:你可以关注我的博客链接。已在 Objective C 和 Swift 中实现。
Swift
Objective C
【讨论】:
非常好。我看到了你的链接,这对初学者非常有用。 我得到了第一个问题的解决方案。现在,我想要的是,当我开始在搜索中输入时,API 必须调用,无论你在其中输入什么,根据其关键字传递给 api 调用,我将在显示列表中显示来自 json 响应的数据. 嗨尼基塔。你可以这样做。在我在搜索结果之后形成数组的方法中。您可以在那里拨打服务电话 当然.. 如果你卡住了。分享代码。你在做什么以上是关于iOS:如何使用目标 c 实现搜索控制器以及如何在运行时调用 API,即当我开始输入搜索控制器时?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ios 9 中以编程方式导航到另一个具有“当前上下文”表示的视图控制器,目标 C
目标 C:使用代码实现导航控制器时如何在 NIB 中添加工具栏
如何在 ios 的 UIactivity 视图控制器中仅显示 facebook、twitter 和电子邮件(目标 c)?