标签:
最近做的项目,有一个函数,百度了一下 结果没 要研究了一下。
当用户输入邮箱形式的账号时,输入完“@”符号后。联想出经常使用的邮箱
点击某一行,将改行代表邮箱自己主动输入到账号输入框内
假设控件属性不懂或者不认识 ,请百度!
- (BOOL)hasPrefix:(NSString *)aString //系统 已经提供了匹配方法,用不着正则! 直接上代码!
#import "UserLoginViewController.h"
@interface UserLoginViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>
{
BOOL _showList;
}@property (nonatomic)UITextField *accountTextField;
@property (nonatomic)UITableView *listTableView;
@property (nonatomic)NSArray *emalArray;//邮箱后缀
@property (nonatomic)NSMutableArray *tabviewData;//server数据
- (void)dealloc
{
[selfunregisterNotifications];
}
- (void)viewDidLoad {
[superviewDidLoad];
[selfregisterNotifications];
_showList = NO;//默认不显示
self.emalArray = [[NSArray alloc] initWithObjects:@"sohu.com",@"sina.com",@"sina.cn",@"163.com",@"126.com",@"qq.com",@"hotmail.com",@"gmail.com", nil];
self.tabviewData = [NSMutableArray array];
_accountTextField= [selfcreateLoginField:@"手机号/username/邮箱/"]; //此处自己定义控件 不会请百度
_accountTextField.frame =CGRectMake(0,0,220,49);
[self.view addSubview:_accountTextField];
//下拉列表
_listTableView = [[UITableViewalloc]initWithFrame:
CGRectMake(0,0,280,120)];
_listTableView.top =50;
_listTableView.left =20;
_listTableView.dataSource=self;
_listTableView.delegate=self;
_listTableView.bounces =NO;
_listTableView.backgroundColor= [UIColorwhiteColor];
_listTableView.separatorColor= [UIColorlightGrayColor];
_listTableView.hidden=!_showList;//一開始listView是隐藏的。此后依据showList的值显示或隐藏
[self.viewaddSubview:_listTableView];
// Do any additional setup after loading the view.
}
-(BOOL)showList{//setShowList:No为隐藏,setShowList:Yes为显示
return_showList;
}
-(void)setShowList:(BOOL)iShow{
_showList=iShow;
_listTableView.hidden=!iShow;
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[[[UIApplicationsharedApplication]keyWindow]endEditing:YES];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//推断text是否输入过@假设输入过则不出现下啦菜单
NSString *text = [textField.textstringByReplacingCharactersInRange:rangewithString:string];
if (textField ==_accountTextField) {
//是否包括@
if ([textcontainsString:@"@"]) {
//抱歉 这是IOS8方法 此处能够替换为 ([text rangeOfString:@"@"].location !=NSNotFound)
[selfsetShowList:YES];
[self.tabviewDataremoveAllObjects];
//范围
NSRange range = [textrangeOfString:@"@"];
if ((range.location + range.length) == text.length) {
for (NSString *strinself.emalArray) {
[self.tabviewDataaddObject:[NSStringstringWithFormat:@"%@%@",text,str]];
}
}else{
NSString *suffix = [textsubstringWithRange:NSMakeRange(range.location+range.length, text.length-(range.location+range.length))];
NSString *headText = [textsubstringWithRange:NSMakeRange(0,range.location+range.length)];
for (NSString *strinself.emalArray) {
//匹配
if ([strhasPrefix:suffix]) {
[self.tabviewDataaddObject:[NSStringstringWithFormat:@"%@%@",headText,str]];
}
}
if (self.tabviewData.count<=0) {
[selfsetShowList:NO];
}
}
[self.listTableViewreloadData];
}else
{
[selfsetShowList:NO];
}
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
//返回一个BOOL值指明是否同意依据用户请求清除内容
//能够设置在特定条件下才同意清除内容
[selfsetShowList:NO];
return YES;
}
#pragma mark 监听键盘
- (void)registerNotifications
{
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification"
object:nil];
}
- (void)unregisterNotifications
{
//移除通知
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
- (void)textFiledEditChanged:(NSNotification *)obj
{
//此处能够拿到 正在输入的值 做一些处理
}
#pragma mark listViewdataSource method and delegate method
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
returnself.tabviewData.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *cellid=@"listviewid";
UITableViewCell* cell=[tableViewdequeueReusableCellWithIdentifier:cellid];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellid];
cell.layer.borderColor = [UIColorgrayColor].CGColor;
cell.layer.borderWidth =1;
}
cell.textLabel.frame =CGRectMake(0,0,220,40);
cell.textLabel.text = [self.tabviewDataobjectAtIndex:indexPath.row];
cell.textLabel.font =_accountTextField.font;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
//当选择下拉列表中的一行时,设置文本框中的值,隐藏下拉列表
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//显示值
NSString *string = [self.tabviewDataobjectAtIndex:indexPath.row];
_accountTextField.text = string;
[selfsetShowList:NO];
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/hrhguanli/p/4671771.html