gpt4 book ai didi

ios - 如何在 IOS 中为下拉菜单元素创建文本过滤器

转载 作者:行者123 更新时间:2023-11-28 12:45:14 25 4
gpt4 key购买 nike

在 android 中,如果我想让用户从一个大列表(100 多个项目)中选择一些东西,我通常会做这样的事情,让用户输入类似自动完成的内容。

enter image description here enter image description here

我想在 iOS 上做类似的事情,但我找不到像我在 Android 上的下拉菜单那样的东西。我意识到我可以在顶部做搜索栏,在它下面做一个表格 View ,但我在它下面有更多的文本字段。

有选择器,但当列表中的项目太多时,它们并不是一个好的选择

我一直在努力寻找一种方法来使用类似这样的东西 https://harvesthq.github.io/chosen/但我是 Xcode 和 iOS 的新手。

最佳答案

我已经尝试过你的问题并得到了解决方案。尝试使用 UISearchBar 和 Table View 最初 TableView 将被隐藏,当用户键入时,将调用搜索栏中的委托(delegate)方法并显示 TableView 。

用户界面应该包含:

  1. 搜索栏(> iOS 8)
  2. 表格 View

适本地连接数据源和委托(delegate)方法

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *orgData;
@property (nonatomic, strong) NSArray *data;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.orgData = @[@"New York, NY", @"Los Angeles, CA", @"Chicago, IL", @"Houston, TX",
@"Philadelphia, PA", @"Phoenix, AZ", @"San Diego, CA", @"San Antonio, TX",
@"Dallas, TX", @"Detroit, MI", @"San Jose, CA", @"Indianapolis, IN",
@"Jacksonville, FL", @"San Francisco, CA", @"Columbus, OH", @"Austin, TX",
@"Memphis, TN", @"Baltimore, MD", @"Charlotte, ND", @"Fort Worth, TX"];

self.data = self.orgData;
[self.tableView setHidden:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.data.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];

cell.textLabel.text = self.data[indexPath.row];

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.searchbar.text = self.data[indexPath.row];
[self.tableView setHidden:YES];
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
[self.tableView setHidden:NO];
return YES;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self.tableView setHidden:NO];
self.data = ([searchText isEqualToString:@""])? self.orgData : [self filterArrayUsingSearchText:searchText];
[self.tableView reloadData];
}

-(NSArray *) filterArrayUsingSearchText:(NSString*) searchText
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];

return [self.orgData filteredArrayUsingPredicate:resultPredicate];
}

关于ios - 如何在 IOS 中为下拉菜单元素创建文本过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38288717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com