2009-08-20 27 views
2

在我的應用程序中有一個tableView &搜索欄。在沒有大小寫的情況下查找類似的字符串 - iPhone

我有一個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 
     } 
} 

在此先感謝您的幫助。

+0

不區分大小寫的字符串比較 – 2009-08-20 16:41:26

回答

2

這有幫助嗎?

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

@marcc - 一個新問題 - 我需要比較而不區分大小寫。 – 2009-08-20 16:40:51

+0

好吧,試試這個沒有Xcode :) if([[dtl uppercaseString] hasPrefix:[strSrch uppercaseString]]) (我相信有一個更有效的方法來做到這一點。你正在查看一個比較小的列表(不是數以萬計的值),如果你的字符串很長,你可能只想比較一個子字符串,這可能會提高性能。 – marcc 2009-08-20 16:47:55

+0

@marcc - 是的! Yahoo!,這是行得通的,大寫的建議能正常工作,感謝您給我的指點, – 2009-08-20 17:06:53

0

請參閱NSArray的-filteredArrayUsingPredicate :.您將傳遞一個NSPredicate對象,該對象使用區分大小寫/變音符比較來比較數組的葉對象各自的字符串屬性。

相關問題