查找类似字符串,不区分大小写 - iPhone

时间:2009-08-20 16:14:09

标签: iphone uitableview

在我的应用程序中有一个tableView&搜索栏。

我有一个NSMutable数组来填充tableView中的数据。

现在,无论用户在搜索栏中输入什么类型 - 数据都应相应地过滤&应重新加载tableView。

我在textField resignFirstResponder上的应用程序中实现了以下代码。

我的问题在此代码中。

-(BOOL)textFieldShouldReturn:(UITextField*)txt
{
      [txt resignFirstResponder];
      // on textfield resign searchData will be called 
      [self searchData];
      return YES;
}
-(void)searchData
{
       // N -> total elements & i for loop
       NSInteger n=[CategoryDtlArray count],i;
       //CategoryDtl is my custom class & it's objects are stored in my array
       CategoryDtl *tmpCat;
       // dtl string -> which is needed for comparison
       NSString *dtl;
       for (i=0; i<n; i++)
       {
               // got the object from array
               tmpCat=(CategoryDtl*)[CategoryDtlArray objectAtIndex:i];
               // got the description from the object for comparison  
               dtl=[NSString stringWithString:tmpCat.Tip_Description];
               // string to search
               NSString *strSrch=[NSString stringWithString:txtSearch.text];
               // now I don't know how to check my object's String
               // is starting with search string or not?
               // if it starts with search string -> it should be displayed in table
               // else not.
               // "How to implement this?"
               // String comparison without case sensitivity
       }
}

先谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

这有帮助吗?

if ([dtl hasPrefix:strSrch])  
{  
    //  match!
}  

答案 1 :(得分:0)

参见NSArray的-filteredArrayUsingPredicate:您将传递一个NSPredicate对象,该对象使用不区分大小写/变音符比较来比较数组的叶对象各自的字符串属性。