سؤال

Okay, after long searching, hesitating and more searching I just can't seem to figure this out.

I have a SearchDisplayController, and I have four different arrays where I am searching in using NSPredicate, because each UITableViewCell is made out of 4 strings (title, description etc.).

I now have 4 search-arrays, search_title, search_description etc which are filled like this:

- (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];

When I use NSLog (array count), I can see it's working because it gives the correct count of each array when I enter a search term.

My tableview cell looks like this:

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];

Maybe you can already see what I'm trying to do. I'm now making new arrays from the old arrays, which I call search arrays. However, those arrays are independent from each other (one array may be empty while the other is filled with researchresults). I need to know from which index the data is, that is stored in those searcharrays. If i know that the data in search_producttitle comes from indexes 1,3 and 4 from price_producttitle (the original array), then I can use those numbers for indexPath.row to show. At least, that's my plan for now, making a searchResult array containing numbers which should be used in the objectAtIndex when making the cell.

I'm struggling with this, I can't find any examples where they are searching within multiple arrays because the cells are made up by multiple arrays.

Can someone please give me a hint in the good direction, or examples which I can use?

Thank you in advance.

Prastow

References I used:

SearchDisplayController search multiple arrays

http://ygamretuta.me/2011/08/10/ios-implementing-a-basic-search-uisearchdisplaycontroller-and-interface-builder/

هل كانت مفيدة؟

المحلول

Although nobody answered my question, I figured it out myself.

Actually, I thought too difficult. I wanted to use NSPredicate which wasn't necessary in my case. I wanted to have a string first from NSPredicate, and then compare the string. I now use rangeofstring, in a loop for every objectatindex, I do this for every array.

If it's found in an array, report YES and the index will be put in a different array. This array will be used later on when making the UITableView, replacing the 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);

I don't take credits for this answer. Credits should go to: SearchDisplayController search multiple arrays

I took this method and implemented it in a -(void)searchtableview. I should mention that there are not a lot of questions about this, and this was the only appropriate answer I could find.

I hope others will find this useful as well!!

نصائح أخرى

-(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];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top