我的应用使用了uisearchbar和uitableview 我能够搜索名称和日期,但是当我搜索日期时,我应该将其填充为“/”,因为它以数据格式存储在该格式中。 我写了一个谓词来做这个搜索。 这就是我的尝试:
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *txt=searchBar.text;
BOOL canEdit=NO;
NSCharacterSet *myCharSet=[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
for (int i=0; i<[txt length]; i++) {
unichar c = [txt characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
canEdit=NO;
}
else {
canEdit=YES;
}
}
if (canEdit) {
searchBar.text=text;
return canEdit;
}
else if(([txt length]==10 && range.location>11))
return NO;
2
txt=[txt stringByReplacingCharactersInRange:range withString:text];
txt=[txt stringByReplacingOccurrencesOfString:@" " withString:@""];
txt=[txt stringByReplacingOccurrencesOfString:@"/" withString:@""];
NSMutableString *mutableText=[txt mutableCopy];
for (NSUInteger i=2; i< mutableText.length && i<6;i+=3) {
[mutableText insertString:@"/" atIndex:i];
}
txt=mutableText;
if(txt.length > 10){
txt=[txt stringByReplacingCharactersInRange:NSMakeRange(10, mutableText.length-10) withString:@""];
}
searchBar.text=txt;
return NO;
}
当我在搜索字段中输入日期时,如何通过自动填充“/”进行搜索。