2009-08-20 12 views
2

私のアプリケーションには、tableview &検索バーがあります。大文字と小文字を区別しない類似の文字列を見つける - iPhone

私はtableViewのデータを埋めるNSMutable配列を持っています。

検索バーにユーザーが入力したとおり、データはそれに応じてフィルタリングする必要があります。& 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 
     } 
} 

ご協力いただきありがとうございます。

+0

大文字と小文字を区別しない文字列比較 - 感度 –

答えて

2

これは役に立ちますか?

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

@marcc - 新しい問題 - 大文字と小文字を区別しないで比較する必要があります。 –

+0

これをXCodeなしで試してみてください:) if([[dtl uppercaseString] hasPrefix:[strSrch uppercaseString]] (これを行うより効率的な方法があります。文字列が長すぎる場合は、部分文字列だけを比較するとパフォーマンスが向上する可能性があります。 – marcc

+0

@marcc - はい! –

0

NSArrayの-filteredArrayUsingPredicate:を参照してください。大文字小文字を区別しない/区別する比較を使用して、配列のリーフオブジェクトのそれぞれの文字列プロパティを比較するNSPredicateオブジェクトを渡します。

関連する問題