I would like to improve research within my UITableViewController.
Now i have an NSMutableArray like this:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"the dog is brown",#"the cat is red",#"the mouse is grey"];
Now if I write in my searchbar for example: "the cat", the table will show "the cat is red" and it is ok.
But if I write: "cat red", the table will be empty.
What can I do to improve this?
so it becomes like making the search now:
-(void)loadArray {
arraySearch = [[NSMutableArray alloc] initWithCapacity:[array count]];
[arraySearch addObjectsFromArray: array];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[arraySearch removeAllObjects];
for (int z=0; z<[array count]; z++) {
if ([[array objectAtIndex:z] rangeOfString:searchText options:NSCaseInsensitiveSearch].location !=NSNotFound) {
[arraySearch addObject:[array objectAtIndex:z]];
}
[self.tableView reloadData];
}
Thanks to all.
I FOUND the solution and I would like to share it with you:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[arraySearch removeAllObjects];
NSArray *listItems = [searchText componentsSeparatedByString:#" "];
NSMutableArray *arrayPredicate = [NSMutableArray array];
for (NSString *str in listItems) {
if ([str length] > 0) {
NSPredicate *pred = [NSPredicate predicateWithFormat:#"self contains [cd] %# OR self contains [cd] %#", str, str];
[arrayPredicate addObject:pred];
}
}
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:arrayPredicate];
[arraySearch addObjectsFromArray:[array filteredArrayUsingPredicate:predicate]];
[self.tableView reloadData];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[arraySearch removeAllObjects];
//Break the sentence into words
NSArray *listItems = [searchText componentsSeparatedByString:#" "];
//Iterate each word and create predicate statement
NSMutableArray *predicatesArray = [NSMutableArray array];
for (NSString *str in listItems) {
[predicatesArray addObject:[NSString stringWithFormat:#"SELF like[c] %#",str]];
}
//Concatenate predicate statements by conditional OR
NSPredicate* predicate = [NSPredicate predicateWithFormat:[predicatesArray componentsJoinedByString:#" OR "]];
//Filter the array against matching predicate
[arraySearch addObjectsFromArray:[array filteredArrayUsingPredicate:predicate]];
[self.tableView reloadData];
}
Related
I am implementing mobile local search in iPhone application:
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
//Remove all objects first.
[copyListOfItems removeAllObjects];
if([searchText length] > 0) {
[ovController.view removeFromSuperview];
searching = YES;
letUserSelectRow = YES;
self.tableView.scrollEnabled = YES;
[self searchTableView];
}
else {
[self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
searching = NO;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
}
[self.tableView reloadData];
}
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:#"key_pdescription"];
[searchArray addObjectsFromArray:array];
}
//in requirement searchArray has more than 50k objects
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
It's working fine for hundreds of records,but I have to implement for 50 thousands or more than that,that search is very very slow after typing 1 character I have to wait to enter next character.
I want to if someone has implemented some optimized algorithm for mobile local search,please guide me how to proceed.
you can use core data and limit the fetch result and populate the data on search result screen,and you can implementat pagination for the next set of records.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"predicateformart"];
[request setPredicate:predicate];
[request setFetchLimit:100];//100 records for example
The problem lies in the method searchTableView.
Firstly: do not create searchArray every time you search, better choice is build it on viewDidLoad.
Secondly: use
[NSArray filteredArrayUsingPredicate:(NSPredicate *)predicate]
instead of manual comparing.
I am trying to add a UISearchBar to my UITableView.
This is the method they use to search:
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:#"Countries"];
[searchArray addObjectsFromArray:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
But I don't have a dictionary, I just want to search in my array 'exerciseArray' which is init like this:
NSString *path = [[NSBundle mainBundle]pathForResource:#"data" ofType:#"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = rootLevel;
[rootLevel release];
When I NSLog it, I get:
exerciseArray: (
{
exerciseName = "Balance Board";
},
{
exerciseName = "Barbell Seated Calf Raise";
},
{
exerciseName = "Calf Press On The Leg Press Machine";
},
{
exerciseName = "Calf Raises- With Bands";
},
So can anyone help me modify the search method to fit the array I have? It seems to have a key in it.
I tried this but it did not work and crashed at line NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
with error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x2d6450
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSLog(#"exerciseArray: %#", exerciseArray);
for (NSString *sTemp in listOfItems)
{
if (sTemp) {
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
}
}
Looks like you have an array of dictionaries with one key/value pair in each dictionary (exerciseName). so for your dataset, it should be this:
for (NSDictionary *dictionary in exerciseArray)
{
NSArray *array = [dictionary objectForKey:#"exerciseName"];
[searchArray addObjectsFromArray:array];
}
...
If you post your VC code related to the array and also your tableview datasource methods, I have can modify it and show you what you need to do.
For searching from table view i have tried this and i got success hope it will help you......
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.searching=NO;
NSString *originalString;
[self.filteredProductArray removeAllObjects];
for (int i=0; i<self.exerciseArray.count; i++)
{
originalString=[[[self.exerciseArray objectAtIndex:i] objectForKey:#"exerciseName"] lowercaseString];
if (!([originalString rangeOfString:[searchText lowercaseString]].location==NSNotFound))
{
[self.filteredProductArray addObject:[self.exerciseArray objectAtIndex:i]];
}
}
if ([searchText isEqual:#""])
{
self.searching=NO;
}
[self.tableView reloadData];
}
I believe NSPredicates are good way to search in arrays.. You can refer to this link.. Using NSPredicate to filter an NSArray based on NSDictionary keys
Can someone exlain this code in detail and suggest how I can sort on the name?
- (void)handleSearchForTerm:(NSString *)searchTerm {
selectButton.enabled = NO;
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in self.keys) {
NSMutableArray *array = [Categories valueForKey:key];
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (NSString *name in array) {
if ([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch].location == NSNotFound)
[toRemove addObject:name];
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
[toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}
- (void)handleSearchForTerm:(NSString *)searchTerm {
selectButton.enabled = NO;
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init]; //creating an mutable array, which can be altered in progress.
[self resetSearch]; //calling some other method not displayed in the code here
for (NSString *key in self.keys) { //for each key,
NSMutableArray *array = [Categories valueForKey:key]; //you get the key's category
NSMutableArray *toRemove = [[NSMutableArray alloc] init]; //and initialize the array for items you wish to remove
for (NSString *name in array) { //then, for each name
if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
[toRemove addObject:name];
//you check if the name is in range of the searchterm, with which you call this function
//if you don't find it, you add it to the removal list
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key]; //if you haven't found any name, it means you've added all the names in the toRemove array
[array removeObjectsInArray:toRemove]; //that means the count of both arrays are the same
[toRemove release]; //so you remove that section entirely, since there is no result there
}
[self.keys removeObjectsInArray:sectionsToRemove]; //you remove all the keys which aren't found
[sectionsToRemove release]; //leaving you the keys which are found
[table reloadData]; //you reload the table with the found results only
}
I hope it all made sense, I did my best commenting it ;)
Good luck.
I have an NSMutableArray displayed in a UITableView, and I have added some elements there.
For example, element names are First, FirstTwo, Second, SecondTwo, Third, ThirdTwo.
Now I want to add a search bar in the screen. In that search bar when I type F, the table should only show First and FirstTwo.
How should I do this?
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
The best way to get the hang of this is by following a tutorial over here over here.
The part you are looking for, is this:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:#""]searchText==nil){
[myTableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in dataSource)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[myTableView reloadData];
}
If you are using search bar. then You can search your contain of table view using delegate method of searchbar
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// myArray is main array for Table View
// tempMainArray which store myArray Data
myArray = [[NSMutableArray alloc]initWithArray:tempMainArray];
NSMutableArray *searchArray = [[NSMutableArray alloc]init];
[searchArray removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:#""]|| searchText==nil){
[myArray removeAllObjects];
myArray = tempMainArray;
[_objTableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in myArray)
{
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names dynamicaly.
{
[searchArray addObject:name];
}
}
counter++;
}
if (searchArray.count > 0) {
[myArray removeAllObjects];
myArray = searchArray;
[_objTableView reloadData];
}
else
{
[myArray removeAllObjects];
myArray = tempMainArray;
[_objTableView reloadData];
}
}
I did it myself and it is working absolutely fine.
Declare two mutable arrays. One containing all the data and second for storing filtered data. Here searchuser is search bar outlet. aray is main array storing all data. set searchbar capitalisation property to sentence so that this would work correctly.
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[searchuser.text lowercaseString]);
filteredarray=[[NSMutableArray alloc]init];
int l=(int)[searchuser.text length];
if (l>0)
{
NSMutableString * data=[[NSMutableString alloc]init];
int a=(int)[aray count];
for (int i=0;i<=a-1;i++)
{
data=[aray objectAtIndex:i];
if ([searchuser.text isEqualToString:data])
{
[filteredarray addObject:data];
}
if ([data length]>l)
{
NSMutableString * string=[[NSMutableString alloc]initWithString:[data substringWithRange:NSMakeRange(0, l)]];
if ([searchuser.text isEqualToString:string])
{
[filteredarray addObject:data];
}
}
}
}
else
{
filteredarray=aray;
}
[tableview reloadData];
}
This code is the search code
- (void) searchTableView {
NSLog(#"4");
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:#"Countries"];
[searchArray addObjectsFromArray:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
//NSLog(#"Count - %d",[copyListOfItems count]);
[searchArray release];
searchArray = nil;
}
I want to implement this type of search in my array but i am getting error in below line.
[searchArray addObjectsFromArray:array];
when control comes on this line application shutdown every time can any one help me ?
Thanks
That line will give you an error only if array has nothing in it(nil value).
So check what is the value of array using nslog.