Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-18-2011, 06:38 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 4
DetartrateD is on a distinguished road
Unhappy UISearchBarController not updateing search results UITableView

Hi everybody!

So, got a table view loaded from core data store with search bar header (set up in IB using UISearchBar + controller) all connections fine.

App runs no problem on devise. Problem is, when search text inputted, results show full table view data and not predicated' data request code as follows;

.m:


Code:
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:nil];
	
    return YES;
}


 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
 {
 return YES;
 }
 

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    [self setSearchIsActive:YES];
    return;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller 
{
    NSFetchRequest *aRequest = [[self fetchedResultsController] fetchRequest];
	
    [aRequest setPredicate:nil];
	
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Handle error
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }  
	
    [self setSearchIsActive:NO];
    return;
}



#pragma mark -
#pragma mark Content Filtering


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
	
    NSFetchRequest *aRequest = [[self fetchedResultsController] fetchRequest];
	
	
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"indexNumber MATCHES[cd] %@", searchText];
	
	self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];
    [aRequest setPredicate:predicate];
	
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }   
}
...

Code:
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] 
							   scope:nil];
	 
	 return YES;
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
	
	[self setSearchIsActive:YES];
}


- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller 
{    
    NSFetchRequest *aRequest = [[self fetchedResultsController] fetchRequest];
	
	[aRequest setPredicate:nil];
	
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }  
    [self setSearchIsActive:NO];
	
    return;
}
 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{
    if ([self searchIsActive]) {
		
        [[[self searchDisplayController] searchResultsTableView] beginUpdates];
    }
    else  {
        [self.tableView beginUpdates];
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{
    if ([self searchIsActive]) {
        [[[self searchDisplayController] searchResultsTableView] endUpdates];
    }
    else  {
        [self.tableView endUpdates];
    }
}
...




I have put all the relevant code snippets in full (hope this helps).

All I want is the search result table to only update with the search results not the whole data set.

I thank you in advance !!

Last edited by DetartrateD; 05-18-2011 at 08:33 AM. Reason: Narrowed issue down to this section of code ;)
DetartrateD is offline   Reply With Quote
Old 05-18-2011, 12:44 PM   #2 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

Quote:
Originally Posted by DetartrateD View Post
All I want is the search result table to only update with the search results not the whole data set.
that's a lot harder than you think. Just calling beginUpdates won't do it. You would need to figure out which rows to delete and which rows to insert on the fly which is ridiculous. I would just call reloadData instead.
__________________
Haters gonna Hate
Likers gonna Like
Bertrand21 is offline   Reply With Quote
Old 05-21-2011, 09:22 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 4
DetartrateD is on a distinguished road
Default

Quote:
Originally Posted by Bertrand21 View Post
that's a lot harder than you think. Just calling beginUpdates won't do it. You would need to figure out which rows to delete and which rows to insert on the fly which is ridiculous. I would just call reloadData instead.
Thank you for the input )

I had to tweak a few bits of code to get it to work.

Solution was sourced from :

iphone - How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar - Stack Overflow

Works a treat now!!
DetartrateD is offline   Reply With Quote
Old 10-24-2011, 05:57 PM   #4 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
roberthuttinger is on a distinguished road
Default post some code!

Quote:
Originally Posted by DetartrateD View Post
Thank you for the input )

I had to tweak a few bits of code to get it to work.

Works a treat now!!
I am running into same issue and cant get it to work.

I am instantiating the UISearch in the .m file, this looks like it would be the solution I need but I cant get it right? Post
Code:
code
please!
roberthuttinger is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, uisearchbardelegate, uisearchdisplaycontroller, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 353
6 members and 347 guests
doffing81, dre, iOS.Lover, Kirkout, PlutoPrime, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0