2011-04-25 44 views
0

我在我的iphone应用程序中实现搜索栏来应用对数组元素的搜索。 在该数组中,我通过解析来保存从服务器获取的数据。 的数据包含了PDF文件的一些像这样的链接:myArray的是一个可变的数组,其内容是:如何在数组元素上应用搜索?

 
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 
http://www.projects-demo.com/iphone/unicurd/recipePagePdf/vwx.pdf 
http://www.projects-demo.com/iphone/unicurd/recipePagePdf/yz1.pdf 

我保存在字符串中searchtext.text 现在什么逻辑应搜索来实现要搜索myArray元素中的searchtext.text?

谢谢。

回答

0

建立一个新的数组如果它属于copyitemcity,则它拥有该值。

搜索代码: -

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]; 
     } 
     } 


} 

试试这个代码进行搜索。

+0

非常感谢!它对我有帮助。 – 2011-04-25 09:42:18

1

你可以这样做: - 让你的数组的名称folderCheckArray(由你改变)。

在搜索栏中searchBUttonclick方法

: -

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar 
{ 
if ([self.folderCheckArray containsObject:theSearchBar.text]) { 
    NSLog(@"array contain object"); 
    } 
else 
{ 
NSLog(@"array contain object"); 
} 
} 

,如果你有在阵列搜索之前,做在搜索文本任何格式,你可以再写入

if([self.folderCheckArray containsObject:[NSString stringWithFormat:@"%@",theSearchBar.text]]) { 
} 
+0

@Jennifer:非常感谢!但以这种方式在SearchBar.text中我们必须输入与数组元素相同的字符串。但根据搜索逻辑,SearchBar.text可以是数组元素的任何子字符串。 – 2011-04-25 07:12:24

+0

@archana如果您使用的是搜索逻辑,那么当您输入字符串时,如果搜索字符串存在于任何数组值中,您可能会得到数组元素的整个值。你需要检查你的搜索代码,你会得到你的解决方案 – Gypsa 2011-04-25 08:18:05

+0

@Jennifer:肯定它会是子串,因为数组包含从服务器获取的URL,并且最终用户不知道/输入整个URL进行搜索。我只想要那个逻辑,如何将字符串作为子字符串与数组元素进行比较。你可以解释这一行如果([self.folderCheckArray containsObject:[NSString stringWithFormat:@“%@”,theSearchBar.text]]){ }意味着什么类型的搜索文本之前搜索它我可以做,可能这会帮助我。谢谢 – 2011-04-25 08:31:01