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.
}
Related
i have a JSON Dict in this format
{
"382" : "sudhir kumar",
"268" : "David ",
"385" : "aayush test",
"261" : "Mike watson",
"277" : "TestDrivers Driver",
"381" : "sudhir kumar",
"380" : "sudhir kumar",
"383" : "asdfgh asdfgh",
"376" : " "
}
I have to display it in UISearchBar. Someone please help me to solve it as when i select any name from search its respective Id also selected
I am using the following code it filtered name but not their related ID's with name
My code is:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
{
tempArray = [NSArray arrayWithArray:DriverNameArray];
//NSString *stringToSearch = textField.text;
tempId = [NSArray arrayWithArray:DriverIdArray];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF BEGINSWITH[c] %#",searchText]; // if you need case sensitive search avoid '[c]' in the predicate
//NSPredicate * predicateid = [NSPredicate predicateWithFormat:#"SELF BEGINSWITH[c] %#",stringToSearch];
NSArray *tempresults = [DriverNameArray filteredArrayUsingPredicate:predicate];
if (tempresults.count > 0)
{
tempArray = [NSArray arrayWithArray:tempresults];
tempId = [NSArray arrayWithArray:tempresults];
}
[SEarchTable reloadData];
//return YES;
}
You have not posted code pertaining to what you've already tried, so I won't post code that directly gives you the solution to your question.
Your best bet is to create an array of dictionaries holding a value for the names and a value for the ID. When you display your data, display the names from the array by myArray[#"name"] convention. You can then get the index of that object and then run [myArray objectAtIndex:myCalculatedIndex][#"id"] to get the ID.
There are actually many ways to solve this problem, but this is one of them.
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:#""];
I use core data with magical record and i'm try to filter data with a search bar in a table view.
I write two methods to get the number of rows and the name of the cells:
-(int) dammiNumeroCercati:(NSString *)searchBar
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"nome CONTAINS [cd] %#", searchBar];
NSArray*arra = [Ricetta MR_findAllSortedBy:#"nome" ascending:YES withPredicate:predicate];
return arra.count;
}
-(NSString*) dammiNomeRicettaCercata:(NSString *)searchBar mostrataNellaCella: (int) cella
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"nome CONTAINS [cd] %#", searchBar];
NSArray *arra = [Ricetta MR_findAllSortedBy:#"nome" ascending:YES withPredicate:predicate];
Ricetta*ctn = arra[cella];
return [NSString stringWithFormat:#"%#", ctn.nome];
}
then i call this method inside the numberOfRowsInSection: and cellForRowAtIndexPath: inside an if cycle:
if (self.mySearchBar.isFirstResponder){
// the above methods
} else {
// the normals methods to have all the data
}
somebody know where I'm wrong or if I miss somethings?
searchBar is usually a UISearchBar, not a string.
You should use searchBar.text and process that in your methods.
Also, in your table view's datasource methods you have to make sure which table view is causing the callback, and then return the correct count/string. Usually this is checked by comparing pointers to the two tables (original table view and search results table view).
-(NSUInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSUInteger)section {
if (tableView == _tableView) {
// return the usual row count
}
return [self dammiNumeroCercati:_searchBar.text];
}
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];