Question

So I have a TableView app where you can favorite certain items with a search bar at the top with a searchDisplayController. I have a push segue from the tableview to another view controller (which is where you do the favoriting/un-favoriting). It is working fine when you favorite/unfavorite something from the regular table view (not from a search), but when you search it doesn't work. It creates a row in the favorites section and does not delete the row in the regular section. Here's my code that's setting favorites/non-favorites

if ([array2 containsObject:self.navigationItem.title]) {
    //favorites already contains the item to favorite
    //delete from favorites section
    [array2 removeObjectIdenticalTo:self.navigationItem.title];
    [[NSUserDefaults standardUserDefaults] setObject:[array2 sortedArrayUsingSelector:
                                                      @selector(localizedCaseInsensitiveCompare:)] forKey:string];
    [[NSUserDefaults standardUserDefaults] synchronize];

    //add to other section
    [theArray2 addObject:self.navigationItem.title];
    [[NSUserDefaults standardUserDefaults] setObject:[theArray2 sortedArrayUsingSelector:
                                                      @selector(localizedCaseInsensitiveCompare:)] forKey:string2];
    [[NSUserDefaults standardUserDefaults] synchronize];

} else {
    //not already in favorites
    //add to favorites
NSLog(@"Title: %@",self.navigationItem.title);
[array2 addObject:self.navigationItem.title];
    [[NSUserDefaults standardUserDefaults] setObject:[array2 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] forKey:string];
    [[NSUserDefaults standardUserDefaults] synchronize];
    //delete from other
[theArray2 removeObjectIdenticalTo:self.navigationItem.title];
    [[NSUserDefaults standardUserDefaults] setObject:[theArray2 sortedArrayUsingSelector:
                                                      @selector(localizedCaseInsensitiveCompare:)] forKey:string2];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

the navigation title is the description that I want in the tableview. I'm sorting it to get in alphabetical order which is what the sortedarrayusingselector is. Any ideas why it would be working when you don't search but when you do it doesn't? Thanks-

Was it helpful?

Solution

The problem was removeObjectIdenticalTo:. Fixed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top