使用textField实现搜索功能

来源:互联网 发布:js函数的封装 编辑:程序博客网 时间:2024/05/16 06:17

作为一个编程小白,这个功能纠结了我几天,边完成公司交给我的任务边想如何实现搜索的思路,终于,要放假了,也就想出来了微笑


还有以后在开发过程中如果没有其他特殊的要求,真的不需要用textField来实现,系统自带的searchBar就很好了,不对,是很棒....(给我自己听的,读者可无视)


首先要想实现搜索功能最核心的就是要去思考textField输入的内容如何去跟数据比较、过滤,从而得到想要的搜索结果。如果你能理清这个,你就完成了大半了

直接上代码吧


创建两个数组,一个是最初显示的原始数据,一个是想要搜索得到的数据,接着创建一个textField当搜索框,和tableView显示视图(TableView和TextField都是用xib创建的),还有TableView的两个代理协议和

@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>{    NSMutableArray *_originalArr;//原始数据...    NSMutableArray *_searchArr;//搜索数据...}@property (weak, nonatomic) IBOutlet UITextField *searchTextField;//搜索textField@property (weak, nonatomic) IBOutlet UITableView *searchTableView;@end



- (void)viewDidLoad {    [super viewDidLoad];        self.title = @"Search";        [self delegate];    [self loadData];    [self textFieldMethod];}



添加搜索数据


//数据源...- (void)loadData {    NSArray *arr = @[@"QA", @"WS", @"ED", @"RF", @"TG", @"HY", @"UJ", @"KI", @"OL", @"P", @"ZA", @"XS", @"CD", @"VF", @"BG", @"NH", @"MJ"];    _originalArr = [NSMutableArray arrayWithCapacity:200];    for (int i = 0; i < [arr count]; i ++) {        [_originalArr addObject:arr[i]];    }}

设置代理方法

//代理方法...- (void)delegate {    //UITableViewDelegate/UITableViewDataSource    self.searchTableView.delegate = self;    self.searchTableView.dataSource = self;    //UITextFieldDelegate    _searchTextField.delegate = self;}


textField....

- (void)textFieldMethod {    [self.searchTextField addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];}

textField的点击事件 ,这里是个重点,因为这里的逻辑判断就决定了你是否能完美实现搜索功能,得到想到的结果

解读一下..

首先判断textField的文本内容,初始化搜索数组,然后遍历原始数据的数组,用textField文本框里面输入的内容和原始数组里面内容str一一比较,进行匹配,我这里做了个判断,如果当textField和str匹配上了,那么结果是true就说明length肯定大于0,然后直接把匹配的数据加入到搜索数组里面,最后刷新tableView

- (void)textFieldDidChange {    if (self.searchTextField.text != nil && self.searchTextField.text.length > 0) {        _searchArr = [NSMutableArray array];//这里可以说是清空tableview的旧dataSource        for (NSString *str in _originalArr) {            if ([str rangeOfString:self.searchTextField.text options:NSCaseInsensitiveSearch].length > 0) {                [_searchArr addObject:str];                    [self.searchTableView reloadData];            }        }    }    [self.searchTableView reloadData];}


设置tableView的行数的时候,也需要判断一下,一个是搜索时的,一个是加载时的。cell的内容也是一样


#pragma mark - tableView- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    if (_searchTextField.text.length > 0 && _searchTextField.text != nil) {                return [_searchArr count];    }else        return [_originalArr count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *idntifi = nil;    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idntifi];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idntifi];    }    if (self.searchTextField.text != 0 && self.searchTextField.text.length > 0) {        cell.textLabel.text = [NSString stringWithFormat:@"%@", _searchArr[indexPath.row]];    }else {        cell.textLabel.text = [NSString stringWithFormat:@"%@", _originalArr[indexPath.row]];    }        return cell;}


最后的效果



0 0
原创粉丝点击