How to use Uisearchbar with data from Core Data?

Asked

Viewed 63 times

0

I’m trying to filter the name of the cells in a table, loaded with data from Core Data, but I’m having a crash.

I am using the following code to filter the cell title:

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

    if (searchText.length == 0) {

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

        // Ajustando valor da flah booleana
        isFiltrado = YES;

        // Alloc e inicia nosso dadosFiltrados
        dadosFiltrados = [[NSMutableArray alloc ]init];

        //Fast enumeration
        for (NSDictionary* nomeAtividade in arrayAtividadeLocal) {

            NSRange rangeNomeAtividade = [[nomeAtividade objectForKey:@"atividade" ]rangeOfString:searchText];

            if (rangeNomeAtividade.location != NSNotFound) {
                [dadosFiltrados addObject: nomeAtividade];
            }
        }
    }

I’m getting the following Xcode error:

Terminating app due to uncaught Exception 'Nsinvalidargumentexception', Reason: '-[Nsmanagedobject objectForKey:]: unrecognized selector sent to instance ...

1 answer

1


Apparently according to the error, in arrayAtividadeLocal you have a vector of objects from Core Data, that’s right?

Therefore, you need to iterate considering this object and not one Nsdictionary, for example:

for (AtividadeLocal *objAtividade in arrayAtividadeLocal) {
    NSString *nomeAtividade = [objAtividade atividade]; // Objeto e propriedade
    NSRange rangeNomeAtividade = [nomeAtividade rangeOfString:searchText];

    if (rangeNomeAtividade.location != NSNotFound) {
        [dadosFiltrados addObject:objAtividade];
    }
}
  • i found the error but then came another logic: The 1st I decided by taking the titles of the objects coming from the core data and passing to a Nsmutablearray> However now, it even finds the object in the search but it always points to the first on the stack of the array :/

  • The funniest that it is now identical to the tutorial I made, and it works normally, but in the implementation of the project this with this problem of presenting the correct data in the search :/

  • You increased the process with this new iteration to have only the names. You can do everything in the same array. Finally, about the other problem, you need to know how the implementation was made.

  • Talk to me on Skype so I can show you better

  • I found the problem, was in cellForRowAtIndexPath, I was still feeding the cells with the original data, and not with the filtered data :D

  • Oh yes, by the method you’re using, I didn’t imagine you were using the UISearchDisplay. For it yes, you have a table with different data only with the search result.

  • Do you know anything about APNS? I just need to create a push notification system and my app will be finished... but I don’t want to use any third party service, I already own a web service... I posted the question in stackoverflow, but if you want we can chat by skype, if you can tbm

Show 2 more comments

Browser other questions tagged

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