Skip to content

zhuzhuxingtianxia/SearchDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SearchDemo

一个用于搜索功能的textField的分类

实现类似web网页的下拉框效果

image

image

实现PopOver功能

image

使用说明

这个搜索小功能是对textField的一个扩展,所以我们首先站到UITextField+PopOver这个扩展类,然后把它添加到工程去。

为了不影响本类的使用,在使用到搜索功能的文件中导入<#import "UITextField+PopOver.h">,哪个textField用到这个功能则可设置定义的属性或方法!

例 1:对文本进行搜索 //需要传入搜索文本所在的数组dataSources

  [field popOverSource:self.dataSources index:^(NSInteger index) {
     //index值是选中的内容在dataSources中的索引
      NSLog(@"dataSources index == %ld",index);

  }];

例 2:对model对象搜索
需要传入搜索对象所在的数组dataSources以及根据哪个属性字段进行搜索
如:一个Person类,有name和address属性,然后我们按照名字搜索 // dataSources为Person的数组容器,根据属性name进行搜索

  [field popOverSource:self.dataSources withKey:@"name" index:^(NSInteger index) {
      //index值是选中的内容在dataSources中的索引
      NSLog(@"dataSources index == %ld",index);

  }];

例 3:textField添加在了scrollView及其子类的容器中的处理
如果field是添加在table的cell上,则需要设置UITextField+PopOver的scrollView属性,用于展示下拉框的位置以及滑动时取消键盘和下拉框的响应。

field.scrollView = self.tableView;
/*
  设置下拉框的位置,默认是根据中心y点做分割,大于y下拉框在field上方显示
  下于y在下方显示。
  ZJPositionTop只在上方显示
  ZJPositionBottom只在下方显示
*/
field.positionType = ZJPositionTop;
 [field popOverSource:self.dataSources  index:^(NSInteger index) {
  NSLog(@"dataSources index == %ld",index);

}];
/*
  //Person对象搜索
  [field popOverSource:self.dataSources withKey:@"name" index:^(NSInteger index) {
   NSLog(@"dataSources index == %ld",index);

 }];
*/

例 4:在某些场景下我们点击textField在不输入内容就要求显示之前输入的内容,或者我们只是选择而不需要输入。
为了实现上述情况新增加了nonInputShow属性,这个属性需要textField的代理方法配合来使用。

 //这种情况下不会弹出键盘,而直接显示下拉框。return YES则下拉框键盘同时显示
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
      textField.nonInputShow = YES;
      return NO;
}

注意:

若textField不是添加在scrollView及其子类的容器中,而且textField所在的视图层次比较多时,则搜索下拉框不显示。

About

一个用于搜索功能的textField的分类

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published