Objective c SearchDisplayController在多个阵列中搜索
好吧,经过长时间的搜索,犹豫和更多的搜索,我似乎无法理解这一点 我有一个SearchDisplayController,我有四个不同的数组,在其中使用NSPredicate进行搜索,因为每个UITableViewCell由4个字符串组成(标题、说明等) 我现在有4个搜索数组,搜索标题、搜索描述等,如下所示:Objective c SearchDisplayController在多个阵列中搜索,objective-c,ios,uitableview,searchdisplaycontroller,Objective C,Ios,Uitableview,Searchdisplaycontroller,好吧,经过长时间的搜索,犹豫和更多的搜索,我似乎无法理解这一点 我有一个SearchDisplayController,我有四个不同的数组,在其中使用NSPredicate进行搜索,因为每个UITableViewCell由4个字符串组成(标题、说明等) 我现在有4个搜索数组,搜索标题、搜索描述等,如下所示: - (void)filterContentForSearchText:(NSString*)searchText scope:(N
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.search_category = [self.temp_category filteredArrayUsingPredicate:resultPredicate];
self.search_producttitle = [self.price_producttitle filteredArrayUsingPredicate:resultPredicate];
self.search_type = [self.price_type filteredArrayUsingPredicate:resultPredicate];
self.search_description = [self.price_description filteredArrayUsingPredicate:resultPredicate];
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.nameLabel.text = [self.search_producttitle objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
cell.priceLabel.text = [self.search_price objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
cell.descrLabel.text = [self.search_description objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
cell.IDLabel.text = [self.search_type objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
}
else{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.nameLabel.text = [price_producttitle objectAtIndex:indexPath.row];
cell.IDLabel.text = [price_type objectAtIndex:indexPath.row];
cell.priceLabel.text = [price objectAtIndex:indexPath.row];
cell.descrLabel.text = [price_description objectAtIndex:indexPath.row];
当我使用NSLog(array count)时,我可以看到它正在工作,因为当我输入搜索词时,它给出了每个数组的正确计数
我的tableview单元格如下所示:
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.search_category = [self.temp_category filteredArrayUsingPredicate:resultPredicate];
self.search_producttitle = [self.price_producttitle filteredArrayUsingPredicate:resultPredicate];
self.search_type = [self.price_type filteredArrayUsingPredicate:resultPredicate];
self.search_description = [self.price_description filteredArrayUsingPredicate:resultPredicate];
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.nameLabel.text = [self.search_producttitle objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
cell.priceLabel.text = [self.search_price objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
cell.descrLabel.text = [self.search_description objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
cell.IDLabel.text = [self.search_type objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
}
else{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.nameLabel.text = [price_producttitle objectAtIndex:indexPath.row];
cell.IDLabel.text = [price_type objectAtIndex:indexPath.row];
cell.priceLabel.text = [price objectAtIndex:indexPath.row];
cell.descrLabel.text = [price_description objectAtIndex:indexPath.row];
也许你已经知道我想做什么了。我现在正在从旧数组中创建新数组,我称之为搜索数组。但是,这些数组彼此独立(一个数组可能为空,而另一个数组则充满Research结果)。我需要知道数据是从哪个索引存储在这些搜索数组中的。如果我知道search\u producttitle中的数据来自price\u producttitle(原始数组)中的索引1、3和4,那么我可以使用这些数字来显示indexPath.row。至少,这是我现在的计划,制作一个包含数字的searchResult数组,在制作单元格时应该在objectAtIndex中使用这些数字
我正在努力解决这个问题,我找不到任何他们在多个数组中搜索的例子,因为单元格是由多个数组组成的
有人能给我一个好的方向的提示,或者我可以使用的例子吗
先谢谢你
普拉斯托
我使用的参考资料:
虽然没有人回答我的问题,但我自己解决了 事实上,我觉得太难了。我想使用NSPredicate,这在我的情况下是不必要的。我想先从NSPredicate中获取一个字符串,然后比较该字符串。我现在使用rangeofstring,在每个objectatindex的循环中,我对每个数组都这样做 如果在数组中找到,则报告YES,索引将放在另一个数组中。稍后在生成UITableView时将使用此数组,替换indexPath.row
[self.searchResults removeAllObjects];
for (int i = 0; i < [temp_category count]; i++) {
BOOL foundResult = FALSE;
if ([[temp_category objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
foundResult = TRUE;
}
if ([[price_producttitle objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
foundResult = TRUE;
}
if ([[price_type objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
foundResult = TRUE;
}
if ([[price_description objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
foundResult = TRUE;
}
if (foundResult) {
NSNumber *result = [NSNumber numberWithInt:i];
if ([self searchResults] == nil) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release];
}
[searchResults addObject:result];
}
}
NSLog (@"array = %i", [searchResults count]);
NSLog(@"%@",searchResults);
[self.searchResults removeAllObjects];
对于(int i=0;i<[temp_类别计数];i++){
BOOL foundResult=FALSE;
if([[temp_category objectAtIndex:i]rangeOfString:searchText].location!=NSNotFound){
foundResult=TRUE;
}
if([[price\u producttitle objectAtIndex:i]rangeOfString:searchText].location!=NSNotFound){
foundResult=TRUE;
}
if([[price\u type objectAtIndex:i]rangeOfString:searchText].location!=NSNotFound){
foundResult=TRUE;
}
if([[price\u description objectAtIndex:i]rangeOfString:searchText].location!=NSNotFound){
foundResult=TRUE;
}
如果(结果){
NSNumber*结果=[NSNumber NUMBER WITHINT:i];
如果([self searchResults]==nil){
NSMUTABLEARRY*array=[[NSMUTABLEARRY alloc]init];
[自设置搜索结果:数组];
[阵列释放];
}
[搜索结果添加对象:结果];
}
}
NSLog(@“数组=%i”,[searchResults count]);
NSLog(@“%@”,搜索结果);
我不认为这个答案值得表扬。学分应分配给:
我采用了这个方法,并在-(void)searchtableview中实现了它。我应该提到的是,关于这一点没有太多问题,这是我能找到的唯一合适的答案
我希望其他人也会觉得这很有用 -(void)搜索栏:(UISearchBar*)搜索栏1 textDidChange:(NSString*)搜索文本
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText
{
if ([searchText length]==0)
{
temp_array1 =[array_Main1 mutableCopy];
temp_array2 =[array_Main2 mutableCopy];
temp_array3 =[array_Main3 mutableCopy];
}
else
{
[temp_array1 removeAllObjects];
[temp_array2 removeAllObjects];
[temp_array3 removeAllObjects];
int g = 0;
for (int i=0; i< array_Main1.count;i++)
{
NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (Range1.location != NSNotFound || Range2.location != NSNotFound || Range3.location != NSNotFound )
{
[temp_array1 addObject:[array_Main1 objectAtIndex:g]];
[temp_array2 addObject:[array_Main2 objectAtIndex:g]];
[temp_array3 addObject:[array_Main3 objectAtIndex:g]];
}
g++;
}
}
[table reloadData];
}
{
如果([searchText length]==0)
{
temp_array1=[array_Main1 mutableCopy];
temp_array2=[array_Main2 mutableCopy];
temp_array3=[array_Main3 mutableCopy];
}
其他的
{
[临时阵列1移除所有对象];
[临时阵列2移除所有对象];
[临时阵列3移除所有对象];
int g=0;
for(int i=0;i