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 withUISearchDisplayController
.– Paulo Rodrigues
Using only
UISearchBar
– Tiago Amaral