How to control the rows between the main UITableView and the UITableView of UISearchBar?

0

I'm implementing a table with a search field, and I'm using UISearchBar .

I have two variables in the app: a NSMutableArray for the original data, and another NSMutableArray for the filtered data.

When the table is loaded, NSMutableArray is used with dadosOriginais , and when I filter something, the found data is placed in NSMutableArray dadosFiltrados . However, when you select the row in the table loaded with the filter, it shows the contents of other information, not the one that appears in the filter.

Understanding better, it seems that when the table is updated with the filtered data, it re-indexes the rows, so for example: the item in the dadosOriginais array that is in position 10 changes its position when it is filtered, position 1 in the dadosFiltrados array.  So when it is selected, it opens the contents of position 1 in the Original array and not in position 10, as it should be.

Has anyone ever had this problem?

Below my code:

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

            static NSString *cellIdentifier = @"Cell";
            UITableViewCell *cell = [tabelaPreview dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            }

   NSManagedObject *processosLocal = [arrayOriginal objectAtIndex:indexPath.row];

        //Exibição de dados quando são filtrados

        if (isFiltrado == YES) {

            // Busca pelo Processo

            // Marca como lido
            if ([[processosLocal   valueForKey:@"lido"] isEqual:@"1"] ) {


                [cell.textLabel setFont:[UIFont systemFontOfSize:16.0f]];
                [cell.detailTextLabel setFont:[UIFont systemFontOfSize:11.0f]];
                [cell.textLabel setText:[NSString stringWithFormat:@"%@", [dadosFiltrados objectAtIndex: indexPath.row]]];

             // Marca como não lido
            }else{

                [cell.textLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
                [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:11.0f]];
                [cell.textLabel setText:[NSString stringWithFormat:@"%@", [dadosFiltrados objectAtIndex: indexPath.row]]];

            }

        // Exibe os dados quando não são filtrados
        }else {

            // Marca como lido
            if ([[processosLocal   valueForKey:@"lido"] isEqual:@"1"] ) {

                [cell.textLabel setFont:[UIFont systemFontOfSize:16.0f]];
                [cell.detailTextLabel setFont:[UIFont systemFontOfSize:11.0f]];
                [cell.textLabel setText:[NSString stringWithFormat:@"%@", [processosLocal valueForKey:@"processo"]]];

                // Marca como não lido
            }else{

                [cell.textLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
                [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:11.0f]];
                [cell.textLabel setText:[NSString stringWithFormat:@"%@", [processosLocal valueForKey:@"processo"]]];

            }
        }
        return cell;
    }

In other words: How can indexPath.row persist from arrayOriginal to arrayFiltrado ?

Below the code responsible for making the filter:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    if (searchText.length == 0) {

        // Ajustando valor da flag booleana
        isFiltrado = NO;
    }else{

        // Ajustando valor da flag booleana
        isFiltrado = YES;

        dadosFiltrados = [[NSMutableArray alloc]init];

        for (NSString* numeroProcesso in [arrayProcessosLocal valueForKey:@"processo"]) {
            NSRange rangeNumeroProcesso = [numeroProcesso rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (rangeNumeroProcesso.location != NSNotFound) {

                [dadosFiltrados addObject:numeroProcesso];

              }
           }

        }
    // Reload nossa table view

    [tabelaPreview reloadData];
}
    
asked by anonymous 06.11.2014 / 17:45

1 answer

1

Your flag isFiltrado , if it is working correctly, can be used to check all methods of the table, for example: numberOfRowsInSection , cellForRowAtIndexPath p>

Then you will define which vector your data will come from. The question is just this one, nothing compared to didSelectRowAtIndexPath but only to data source .

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSManagedObject *processosLocal;

    if (isFiltrado) {
        processosLocal = [arrayFiltrado objectAtIndex:indexPath.row];
    } else {
        processosLocal = [arrayOriginal objectAtIndex:indexPath.row];
    }
}

Another example:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (isFiltrado) {
        return [arrayFiltrado count];
    } else {
        return [arrayOriginal count];
    }
}
    
06.11.2014 / 18:27