I'm using UISearchBar in my app.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filtered removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.companyName contains[cd] %#",searchText];
NSArray *tempArray = [ee filteredArrayUsingPredicate:predicate];
filtered = [NSMutableArray arrayWithArray:tempArray];
}
I did NSLog on above "filtered" array, it's getting proper search results.
I don't see the filtered values in the tableview. I see "No results" if I type wrong search text. But for the correct search text, the table view is plain empty.
Can anyone help me what needs to be done?
Thanks in advance!!
Please use the following code . And make sure that self.filtered is the datasource of yourtableview . Datasource means array
from tableview gets populated
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filtered removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.companyName contains[cd] %#",searchText];
NSArray *tempArray = [ee filteredArrayUsingPredicate:predicate];
filtered = [NSMutableArray arrayWithArray:tempArray];
[yourtableview reloadData];
}
Write the above code then Reload the table in the source code
[tableView reloadData];
Related
To test out a restaurant searching app I included 4 test restaurants in a JSON file which populate a table view correctly with their corresponding properties. In 3 text fields I filter the array with a name, average entree price and rating, yet when I pass it to the method the filtered array logged is not filtered. I don't see anything wrong with my predicate code though, any ideas? Thank you!
- (void)searchRestaurantsWithName:(NSString *)name price:(int)price andRating:(int)rating completion:(void (^)(NSArray *restaurants))completion {
NSMutableArray *restaurantsToFilter = [[RestaurantController sharedInstance].restaurants mutableCopy];
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:#"name CONTAINS[c] %#", name];
NSPredicate *pricePredicate = [NSPredicate predicateWithFormat:#"price < %i", price];
NSPredicate *ratingPredicate = [NSPredicate predicateWithFormat:#"rating > %i", rating];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:#[namePredicate, pricePredicate, ratingPredicate]];
NSArray *filteredArray = [restaurantsToFilter filteredArrayUsingPredicate:compoundPredicate];
completion(filteredArray);
}
The method the button calls
- (void)filterArray {
[self searchRestaurantsWithName:self.nameTextField.text price:[self.priceTextField.text intValue] andRating:[self.ratingTextField.text intValue] completion:^(NSArray *restaurants) {
[self.tableView reloadData];
NSLog(#"%#", restaurants);
}];
}
I'm trying to get all the values of keys that are only in an array after filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
if ([scope isEqualToString:#"Calendar"]) {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#", searchText];
self.searchResults = [NSMutableArray arrayWithArray:[[allsearchDictKeys filteredArrayUsingPredicate:resultPredicate] sortedArrayUsingSelector:#selector(localizedStandardCompare:)]];
}
}
How can I get an array, that has the values for the keys in self.searchResults array only, with the same indexPaths as their counterpart?
This is being passed into my UISearchResults updating tableView, so I would like this to occur prior to the update so I don't have to do much in cellForRowAtIndexPath. I know I can enumerate through them, and add those objects to another array, but my question is can I bypass that and just simply directly add them to an array?
Example:
self.searchResultsValuesArray = [searchDict valuesForKeys:self.searchResults];
I know there is no valuesForKeys but it's there for illustrative purposes.
You can use the NSDictionaryMethod objectsForKeys:notFoundMarker:
self.searchResultsValuesArray = [searchDict objectsForKeys:self.searchResults notFoundMarker:#""];
My filteredContentForSearchText ignores predicate after user presses cancel on search bar in iOS.
I am filtering an array with a uisearchbar with the following method:
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredUsersArray removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.fullName contains[c] %#",searchText];
self.filteredUsersArray = [NSMutableArray arrayWithArray:[self.usersArray filteredArrayUsingPredicate:predicate]];
if ([self.filteredUsersArray count] > 0) {
}else{
User *tempUser = self.addedUsersArray[0];
tempUser.fullName = [NSString stringWithFormat:#"Search for '%#'", searchText];
[self.filteredUsersArray addObject:tempUser];
}
}
Everything works perfect until a user presses cancel or clears there search. After that action occurs this line no longer matches and populates the array:
self.filteredUsersArray = [NSMutableArray arrayWithArray:[self.usersArray filteredArrayUsingPredicate:predicate]];
Does anyone know why the array would return a count of zero after cancel is pressed even when the criteria matches?
Actually am getting datas from JSON webservice. I need to search data from UITableView cells using uisearchdisplaycontroller.
I have passed data into cells successfully using NSMutablArray with multiple array. But now i need to search data from that.
My array:
[
{
"name": "jhon",
"city": "chennai",
}
{
"name": "Micle",
"city": "Newyork",
}
{
"name": "Micle",
"city": "Washigton",
}
]
My custom cells in UITableView:
cell.P_name.text = [details objectForKey:#"name"];
cell.P_city.text = [details objectForKey:#"city"];
In Search i tried :
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
//-- Help to customize operations
searchResults = [[NSArray alloc]initWithArray:mainArray];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF contains[cd] %#", searchText];
searchResults = [details filteredArrayUsingPredicate:resultPredicate];
[tableView reloadData];
}
Can anybody help to resolve my issue.
You should have 2 arrays:
Your main store of data from your JSON (mainDataList)
Your data source array used to populate your table view(s) (dataSourceList)
Initially:
self.dataSourceList = self.mainDataList;
because you are displaying all of the data in your table view. When any search is cancelled you also go back to this state (then reload).
When searching however, you need to filter the contents of the dataSourceList and reload the table:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"name contains[cd] %#", searchText];
self.dataSourceList = [self.mainDataList filteredArrayUsingPredicate:resultPredicate];
[tableView reloadData];
}
NOTE: the above predicate only searches the name in your dictionaries...
Now, all of your table view methods only use self.dataSourceList, and you modify its contents based on your state. You code is clean and simple. Smile :-)
I just put my logic take one more mutable array in .h file name is _temArray
And add your _mainArray (populated on tableView) to your _temArray such like,
[_temArray addObjectsFromArray:_mainArray];
In Search method use NSPredicate:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
//-- Help to customize operations
if([searchText length] > 0)
{
NSMutableArray *fileterMainArray = (NSMutableArray *)_temArray
NSPredicate *predicatePeople = [NSPredicate predicateWithFormat:#"name BEGINSWITH[cd] %#", searchText]; // here if you want to search by city then change "name" to "city" at pattern.
NSArray *filteredArray = [fileterMainArray filteredArrayUsingPredicate:predicatePeople];
[_mainArray addObjectsFromArray:filteredArray];
}
else
[_mainArray addObjectsFromArray:_temArray];
[self.tblView reloadData]; // don't forget to reload Table data.
}
I have some arrays whose contents are picked from db.I want to match up the text i have entered in 'searchTextField' with those array's contents which is taken from DB.
For example: my array contains, myArray={'bat man','bat and ball','ball'};
if i have entered 'bat' in 'searchTextField'; it must show index of matching text in array(in this case index 0 and 1).
How can i achieve this..
Waiting for your help..
NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:#"bat man",#"bat and ball",#"ball", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains[c] 'bat'"];
NSArray *result = [tempArray filteredArrayUsingPredicate:predicate];
result array will be containing the filtered objects, from there you can get the index as:
[tempArray indexOfObject:/the object from result array, one by one/]
contains[c] means search will be case insensitive. For more on predicates: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html
EDIT
set the textField's delegate as self. Before that go to YourFile.h, there add UITextFieldDelegate. now in textFieldShouldReturn do this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:#"bat man",#"bat and ball",#"ball", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#",textField.text];
NSArray *result = [tempArray filteredArrayUsingPredicate:predicate];
return YES;
}