iOS:如何使用 objective 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?

我想实现类似 gmail 应用程序的搜索控制器,

我想像这个例子一样实现

图 1 它显示带有搜索控制器按钮的正常收件箱

图2点击搜索按钮后会像图2一样展开显示,这里我们可以输入文字...等等

图 3 这里无论我们输入什么,取决于它在列表中显示的文本,当我们点击它时,它将导航到详细视图。

我想实现同样的事情。

唯一的问题是,当我开始在搜索控制器中输入时,一个 API 将调用即搜索,然后根据在搜索中输入的文本,它将作为参数并在列表中显示数据(table 视图)。此数据将来自 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];
}

你可以关注我的博客link。已在 objective C 和 Swift.

中实现

Swift

Objective C

按照下面的步骤操作,肯定不会和你截图的一模一样。

第 1 步: 在导航栏上创建一个导航按钮,并为其添加一个搜索图像按钮。这可以通过使用故事板或以编程方式使用来完成。

对该按钮执行操作,

- (IBAction)searchButtonAction:(id)sender {

    SearchViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"id1"];
    [self.navigationController pushViewController:vc animated:YES];

}

单击此按钮后,它将导航到 SearchViewController 视图。

看故事板,设计如下图

并在 SearchViewController 中编写以下代码,

创建 textField 的 IBOutlet 并为搜索按钮提供操作,并添加table查看并为其添加委托和数据源。

  @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 解析并编写 table 查看数据源和委托方法并显示数据。