ios - 如何在 iOS 中实现如下图所示的搜索功能?

标签 ios objective-c uitableview uisearchbar uisearchcontroller

enter image description here

以上图片是 Gmail 应用程序的示例。

我想要的是,

图 2 - 我想显示这样的建议列表,当用户点击该建议时,它必须根据该搜索值导航到下一个 View

图 3 A - (打字前)当我点击搜索栏按钮时,就像我想显示的这个 View 一样,我也想显示分段控件,如图中所示。

图 3 B - 当用户根据搜索值开始输入时,数据必须显示在表格 View 中。

我不太了解 TableView 及其功能。我想了解如何在 iOS 中实现此功能。

任何人都可以帮助我实现此功能。

最佳答案

第 1 步:简单地创建一个搜索导航按钮

    UIButton *searchButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setImage:[UIImage imageNamed:@"search1"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [searchButton setFrame:CGRectMake(10, 0, 35, 35)];

     UIView *rightBarButtonItems = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 76, 32)];
    [rightBarButtonItems addSubview:searchButton];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtonItems];

第 2 步:对其执行操作后,编写用于导航下一个 View 的代码

- (IBAction)searchButtonClicked {

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

    }

第 3 步:取一个文本字段和如下图所示的分段控件,并在分段控件下方添加 2 个表格 View 并编写代码。 enter image description here

// This method used to allow select segmented button multiple times (multi-select segmented control)

-(void)setMultiSelectControl:(MultiSelectSegmentedControl *)multiSelectControl{
    _multiSelectControl = multiSelectControl;
    self.multiSelectControl.tag = 2;
    self.multiSelectControl.delegate = self;
}

-(void)multiSelect:(MultiSelectSegmentedControl *)multiSelecSegmendedControl didChangeValue:(BOOL)value atIndex:(NSUInteger)index{

if(index==0)
    {

    _multiSelectControl.selectedSegmentIndex=0;
    _tableview1.hidden=NO;
    _tableview2.hidden=YES;

    [_seachTextField resignFirstResponder];

    // your code

}

if (index==1) {

    _multiSelectControl.selectedSegmentIndex=1;

    _tableview1.hidden=YES;
    _tableview2.hidden=NO;

    [_seachTextField resignFirstResponder];

     // your code

}

}

关于ios - 如何在 iOS 中实现如下图所示的搜索功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48146788/

相关文章:

ios - 从 UITableViewCell.subviews Swift 访问 "SeparatorView"

iphone - 更改方向时如何正确调整 UIView 的大小?

ios uiviewcontroller uitabbarcontroller 的 child

ios - 在 iOS 7 上本地验证应用内收据和捆绑收据的完整解决方案

ios - fatal error : Index out of range when using UITableView with multiple sections

ios - UITableView 意外发现 Nil 不一致发生

iPhone - 保持表格 View 状态的最简单方法?

ios - iOS 7开发人员-最小化应用会导致UIWebView嵌入式YouTube播放器停止播放

iphone - 无法在动画 block 中的循环上设置动画

iphone - 如何检查iphone应用程序中的更大值(value)