I am implementing search bar in my iphone app to apply searching on array element. In that array I save the data fetched from server through parsing. The data contains some links for pdf files like this: myArray is an mutable array and its elements are :
http://www.projects-demo.com/iphone/unicurd/recipePagePdf/abc.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/def.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/ghi.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/jkl.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/mno.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/pqr.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/stu.pdf开发者_C百科 http://www.projects-demo.com/iphone/unicurd/recipePagePdf/vwx.pdf http://www.projects-demo.com/iphone/unicurd/recipePagePdf/yz1.pdf
I am saving the string to be searched in searchtext.text Now what logic should be implement for searching this searchtext.text in myArray's element ?
Thanks.
You can do this:- let name of your array folderCheckArray(change by yours).
in your search bar searchBUttonclick method:-
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
if ([self.folderCheckArray containsObject:theSearchBar.text]) {
NSLog(@"array contain object");
}
else
{
NSLog(@"array contain object");
}
}
if you have to do any formatting in the search text before searching it in array you can then write
if([self.folderCheckArray containsObject:[NSString stringWithFormat:@"%@",theSearchBar.text]]) {
}
Make a new array that holds the value if foundlet it be copyitemcity.
Searching code:-
NSString *searchText = searchBar.text;
for (NSString *str in folderCheckArray)
{
NSRange titleResultsRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(titleResultsRange.location != NSNotFound)
{
if (titleResultsRange.location==0)
{
[copyitemcity addObject:str];
}
}
}
Try this code for searching.
精彩评论