如何在搜索列表中首先获得相同名称的结果 iOS
How to get same name results first in search list iOS
大家好,我需要你们的帮助。我希望像图像中那样,如果我搜索胡萝卜列表应该首先显示胡萝卜,就像首先显示具有相同名称的元素,如果元素包含这些文本列表,那是可能的
NSString *predicateString;
NSString * tempString;
if (string.length > 0) {
tempString = [NSString stringWithFormat:@"%@%@",textField.text, string];
} else {
tempString = [textField.text substringToIndex:[textField.text length] - 1];
}
predicateString = [NSString stringWithFormat:@"SELF.title contains [cd] \"%@\" ", tempString];
NSLog(@"Ingredent Array :- %@ ",allIngrediantArr);
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
if (allIngrediantArr.count>0) {
searchFilterdArr = [NSMutableArray arrayWithArray:[allIngrediantArr filteredArrayUsingPredicate:predicate]];
}
你可以备用
开始
predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
包含 ||开始
如果你想要两者(包含||开头),那么使用
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title CONTAINS [cd] \"%@\")", tempString, tempString];
匹配 ||开始
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
查看 NSPredicate
string comparison
您可以通过比较搜索字符串的范围来过滤searchFilterdArr
数组。
NSString *searchStr = @"carrot";
NSArray *searchFilterdArr = [[NSArray alloc ] initWithObjects:@{@"title":@"baby carrot"},@{@"title":@"baby purple carrot"},@{@"title":@"carrot"}, nil];
NSLog(@"%@",searchFilterdArr);
id mySort = ^(NSDictionary * obj1, NSDictionary * obj2){
return [[obj1 valueForKey:@"title"] rangeOfString:searchStr].location > [[obj2 valueForKey:@"title"] rangeOfString:searchStr].location;
};
NSArray * sortedMyObjects = [searchFilterdArr sortedArrayUsingComparator:mySort];
NSLog(@"%@",sortedMyObjects);
searchFilterdArr
{
title = "baby carrot";
},
{
title = "baby purple carrot";
},
{
title = "carrot";
}
sortedMyObjects
{
title = "carrot";
},
{
title = "baby carrot";
},
{
title = "baby purple carrot";
}
大家好,我需要你们的帮助。我希望像图像中那样,如果我搜索胡萝卜列表应该首先显示胡萝卜,就像首先显示具有相同名称的元素,如果元素包含这些文本列表,那是可能的
NSString *predicateString;
NSString * tempString;
if (string.length > 0) {
tempString = [NSString stringWithFormat:@"%@%@",textField.text, string];
} else {
tempString = [textField.text substringToIndex:[textField.text length] - 1];
}
predicateString = [NSString stringWithFormat:@"SELF.title contains [cd] \"%@\" ", tempString];
NSLog(@"Ingredent Array :- %@ ",allIngrediantArr);
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
if (allIngrediantArr.count>0) {
searchFilterdArr = [NSMutableArray arrayWithArray:[allIngrediantArr filteredArrayUsingPredicate:predicate]];
}
你可以备用
开始
predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
包含 ||开始
如果你想要两者(包含||开头),那么使用
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title CONTAINS [cd] \"%@\")", tempString, tempString];
匹配 ||开始
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
查看 NSPredicate
string comparison
您可以通过比较搜索字符串的范围来过滤searchFilterdArr
数组。
NSString *searchStr = @"carrot";
NSArray *searchFilterdArr = [[NSArray alloc ] initWithObjects:@{@"title":@"baby carrot"},@{@"title":@"baby purple carrot"},@{@"title":@"carrot"}, nil];
NSLog(@"%@",searchFilterdArr);
id mySort = ^(NSDictionary * obj1, NSDictionary * obj2){
return [[obj1 valueForKey:@"title"] rangeOfString:searchStr].location > [[obj2 valueForKey:@"title"] rangeOfString:searchStr].location;
};
NSArray * sortedMyObjects = [searchFilterdArr sortedArrayUsingComparator:mySort];
NSLog(@"%@",sortedMyObjects);
searchFilterdArr
{ title = "baby carrot"; }, { title = "baby purple carrot"; }, { title = "carrot"; }
sortedMyObjects
{ title = "carrot"; }, { title = "baby carrot"; }, { title = "baby purple carrot"; }