searchTextField=[[UITextFieldalloc]initWithFrame:CGRectMake(30,0,searchView.frame.size.width-120,30)];
[searchTextFieldsetPlaceholder:MYLocalizedString(@"shangpinmingchengtiaoma",@"")];
searchTextField.font = SearchViewTextFont;
[searchTextFieldsetBackgroundColor:[UIColorwhiteColor]];
// searchTextField.layer.borderWidth = 1;
// searchTextField.layer.borderColor = COLOR(173, 173, 173, 1).CGColor;
searchTextField.delegate=self;
[searchView addSubview:searchTextField];
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextFieldsetAutocapitalizationType:UITextAutocapitalizationTypeNone];//关闭首字母大写
[searchTextFieldsetAutocorrectionType:UITextAutocorrectionTypeNo];//关闭键盘联想
//设置 UITextField 输入监听事件
[searchTextFieldaddTarget:selfaction:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];
searchTextField.text =self.searchStr;
[searchTextFieldbecomeFirstResponder]
//
//
//
//
UITextField *searchTextField;//搜索
NSTimeInterval textFieldDidChangeInterval;
NSString *tempString;
-(void)textFieldDidChange:(id)sender{
UITextField *senderText=(UITextField *)sender;
// NSString *textSearchStr = [senderText.text stringByReplacingOccurrencesOfString:@" " withString:@""];
//注:iOS区分中英文空格 stringByReplacingOccurrencesOfString替代为如下方法
// NSString *textSearchStr = [[senderText.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
//去除首尾空格
NSString *textSearchStr = [senderText.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
//中文输入时候会连续掉用两次 (在数据量大的时候)从而出现崩溃问题 故:检测字符相同时候就return
if ([self.searchStrisEqualToString:textSearchStr])return;
//限制字符输入长度
if (textSearchStr.length >textSearchCount) {
searchTextField.text = [textSearchStrsubstringToIndex:textSearchCount];
[searchTextFieldendEditing:YES];
return;
}
NSTimeInterval start =CACurrentMediaTime();
//获取用户输入间隔时间 如果间隔时间太短 < 0.8则只刷新用户最后一次输入的字符
if (start -textFieldDidChangeInterval <0.5) {
textFieldDidChangeInterval = start;
//如果0.5秒之后 用户没有输入 则刷新列表
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
if ([tempStringisEqualToString:textSearchStr]) {
NSLog(@"刷新列表");
}else{
NSLog(@"不刷新");
}
});
if (![tempStringisEqualToString:textSearchStr]) {
tempString = textSearchStr;
}
}else{
textFieldDidChangeInterval = start;
NSLog(@"刷新列表");
}
}