How to control the lines between the main Uitableview and the Uitableview of the Uisearchbar?

Asked

Viewed 148 times

0

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

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

When the table is loaded, the NSMutableArray with the dadosOriginais, and when I filter something, the data found are placed in the NSMutableArray dadosFiltrados. However, when the row in the table loaded with the filter is selected, it shows the contents of other information, not the one shown in the filter.

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

Someone’s had this problem before?

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 to persist arrayOriginal in the 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];
}
  • You’re using a simple UISearchBar or implementing the complete solution with UISearchDisplayController.

  • Using only UISearchBar

1 answer

1


Your flag isFiltrado, if it is working correctly, it can be used for checking in all table methods, for example: numberOfRowsInSection, cellForRowAtIndexPath and didSelectRowAtIndexPath.

Then, you will define which vector your data will come from. The question is just this, nothing with respect to indexPath but only to the 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];
    }
}
  • Yes yes, but I’m trying to make that same logic in didSelectRowAtIndexPath: because that is where the content is pointed

  • Right, but logic has to be applied to all methods of the data source. What’s the problem in this that you tried?

  • The problem is that it correctly shows only one object item, the name, but does not correctly load the other items that did not enter the search. For example imagine dataSourceOriginal with 3 objects. Position 1 |B position 2 |C position 3 So vc makes a filter in the element name and returns C, but in the filter array, C is no longer at position 3, in the new filter it is at position 1. So when it is selected, uitableview, takes the organization of the filtered item array, and points Nod datasource to position 1, which falls on top of element A data.

  • Ah has an OBS tbm, the data is coming from Core Data

  • The problem is not then in the view but in the filter, at the moment you create this new array. But reinforcing, that by its code, the display still seems incorrect.

  • Good for what I already have the filter display is fine, and it responds to correct searches. And that’s exactly it, in case I would have to replicate the new array not only with an element, but with the filtered objects. These objects come from the core data. I will rephrase the issue with the code that makes the filter.

  • Okay, it already includes the code that makes the filter.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.