UISearchBar that Searches Entire UITableView Cell - ios

I have a tableview that filters based on a search bar. This works well as long as the search begins with the same characters as the cell text.
I would like to be able to search any part of the cell text, even in the middle. For example. If the cell text is "Jon Bon Jovi" I would like it to still come up if the user types "Bon Jovi" or even just "Jovi" into the search bar.
My current code is below:
for (NSDictionary *item in listItems)
{
if ([scope isEqualToString:#"All"] || [[item objectForKey:#"type"]
isEqualToString:scope] || scope == nil)
{
NSComparisonResult result = [[item objectForKey:#"name"]
compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[filteredListItems addObject:item];
}
}
}
Any help would be awesome. Thank you all!

Instead of using -compare:options:range: (which just sorts your search string relative to your item's name), consider looking at -rangeOfString:options: - that will actually search the entire item name for your search string. You would replace the inner body of your if statement with something like:
NSStringCompareOptions opts = (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch);
NSRange resultRange = [[item objectForKey:#"name"] rangeOfString:searchText
options:opts];
if (resultRange.location != NSNotFound) {
[filteredListItems addObject:item];
}

Related

Search in iOS for custom objects

I am working on a custom contact,currently i'm using this code to search custom objects
filteredTableData = [[NSMutableArray alloc] init];
for (int i=0; i<contactSectionTitles.count; i++)
{
NSString *sectionTitle = [contactSectionTitles objectAtIndex:i];
NSArray *sectionmContacts = [mContacts objectForKey:sectionTitle];
for (CS_ContactDTO *theDto in sectionmContacts) {
NSRange nameRange = [theDto.mFirstName rangeOfString:text options:NSForcedOrderingSearch];
NSRange descriptionRange = [[theDto.mFirstName description] rangeOfString:text options:NSForcedOrderingSearch];
if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
{
if (![filteredTableData containsObject:theDto])//Search DTO
{
[filteredTableData addObject:theDto];
}
}
}
}
Note
"theDTO" is my custom class
It has values FirstName,LastName,PhoneNumber.
The code gives me result like,
For "A" -> "Arun","India","Kumar"
My requirement is like to search
"A" should provide only "Arun"(starting letter matching),not the other results.
Simply change your check to see if the range location is 0:
if (nameRange.location == 0 || descriptionRange.location == 0) {
This will be true if text is at the start of the name or description.
BTW - why do you check both mFirstName and mFirstName description? What's the difference?
If you want to check if NSString starts with a particular search term, you can use
if ([myString hasPrefix:#"searchTerm"])

find a letter anywhere in an array of strings ios

I have the method below for filtering some results:
Currently it does this:
array: alpha, apple, aries, bravo
type a:
alpha
apple
aries
type l (now al)
alpha
I wanted to do this:
new search:
type p=
alPha
apple
below is the code
thank you kindly in advance
-(void) filterResults:(NSString *)searchText{
NSMutableArray *test = [NSMutableArray arrayWithArray:self.listContent];
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (int i=0; i<[test count]; i++) {
NSString *stringResult = [test objectAtIndex:i];
NSComparisonResult result = [stringResult compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[self.filteredListContent addObject:stringResult];
}
}
[self.filteredListContent sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];//sort alphabetically
NSLog(#"filtered results = %#",self.filteredListContent);
}
To find the letter anywhere in your string, replace this:
NSComparisonResult result = [stringResult compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[self.filteredListContent addObject:stringResult];
}
with this:
NSRange range = [stringResult rangeOfString:searchText];
if (range.location != NSNotFound) {
[self.filteredListContent addObject:stringResult];
}
Note that it will have to find all of the characters, in order.
If you want to find any of the characters, then use rangeOfCharacterFromSet:.
Try this one.
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"SELF beginswith '%#'", searchText]];
self.filteredListContent = [self.listContent filteredArrayUsingPredicate:predicate];
Hope this helps you.

Search display controller iOS results disappear then reappear between words

I am searching my products like the below. It works fine however when searching between 2 words the results disappear then reappear once the user has entered the first character of the next word. i.e if I search "td pro" or "pro td" the results are there. If I search like this td(i have a result) td(space) I have NO result td p(I have a result) just to add I need to separate the words by space in componentsSeperatedByString because the user may not search for a product in any particular order. Unless there is a better method?
Here is my code:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (XMLStringFile *new in rssOutputData_MutableArray)
{
//Product scope
if ([scope isEqualToString:#"Product"])
{
// Split into search text into separate "words"
NSArray * searchComponents = [searchText componentsSeparatedByString: #" "];
;
BOOL foundSearchText = YES;
// Check each word to see if it was found
for (NSString * searchComponent in searchComponents) {
NSRange result = [new.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound);
}
// If all search words found, add to the array
if (foundSearchText)
{
[self.filteredListContent addObject: new];
}
}
}
}
Many thanks
I fixed it with the help of another stack question that someone helped me with.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (XMLStringFile *new in rssOutputData_MutableArray)
{
//Product scope
if ([scope isEqualToString:#"Product"])
{
// Split into search text into separate "words"
NSArray * searchComponents = [searchText componentsSeparatedByString: #" "];
//Before searching trim off the whitespace
searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
BOOL foundSearchText = YES;
// Check each word to see if it was found
for (NSString * searchComponent in searchComponents) {
NSRange result = [new.xmlItem rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
NSRange descriptionRange = [new.xmlDescription rangeOfString:searchComponent options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
foundSearchText &= (result.location != NSNotFound|| descriptionRange.location != NSNotFound);
}
// If all search words found, add to the array
if (foundSearchText)
{
[self.filteredListContent addObject: new];
}
}
}
}
The winning line is:
//Before searching trim off the whitespace
searchText = [searchText stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

iOS - Searching Table View

This is my current search code.
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {
[filteredList removeAllObjects];
for(Location *item in list)
{
NSRange result = [item.title rangeOfString:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
if(result.location != NSNotFound)
{
[filteredList addObject:item];
}
}
}
How would I change this to match any part of the title and not just the start.
Example:
At the moment if I search for 'Las', 'Las Vegas' will appear in the list, but when I search for 'Vegas' it does not.
I want the search term 'Vegas' to come back with 'Las Vegas'
Thanks,
Ashley
Use rangeOfString:options: instead of rangeOfString:options:range:. It is the range parameter that restricts the search to the beginning of the string.
Or if for some reason you want to use rangeOfString:options:range: then use this as range: NSMakeRange(0, [item.title length])

UIScope Bar doesn't show items (if no Searchtext is submitted)

i have a Searchbar with a Scopebar in my RootViewController. If I am searching for the items it's working very fine, also if I searching in a Scope Range. But how is it possible to show Scope Results, if no Searchtext was given from the User?
A Code Sample would be very fine.
Here is the Code which filter after the User set Text into the Seachbar.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredContent removeAllObjects]; // First clear the filtered array.
for (test *new in tabelle)
{
//cat1
if ([scope isEqualToString:#"cat1"] && [new.Location isEqualToString:#"cat1"])
{
NSComparisonResult result = [new.TText compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredContent addObject:new];
}
}
//cat2
if ([scope isEqualToString:#"cat2"] && [new.Location isEqualToString:#"cat2"])
{
NSComparisonResult result = [new.TText compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredContent addObject:new];
}
}
//all
if ([scope isEqualToString:#"Alle"])
{
NSComparisonResult result = [new.TText compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredContent addObject:new];
}
}
}
}
I want results without any typed letter in the Searchbar
Like this image but without the typed in letter: http://edumobile.org/iphone/wp-content/uploads/2010/03/search.jpg

Resources