views:

164

answers:

2

In my application there is a tableView & a search bar.

I have an NSMutable array to fill data in tableView.

Now, whatever user types in a search bar - data should be filter accordingly & tableView should be reloaded.

I have implemented following code in my application on textField resignFirstResponder.

My question is within this code.

-(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
       }
}

Thanks in advance for helping me.

+1  A: 

Does this help?

if ([dtl hasPrefix:strSrch])  
{  
    //  match!
}
marcc
@marcc - A new Problem - I need to compare without case - sensitivity.
sugar
ok, trying this without XCode :)if ([[dtl uppercaseString] hasPrefix: [strSrch uppercaseString]])(I'm sure there's a much more efficient way to do this. This will work and be fine if you are looking at a relatively small list (not tens of thousands of values) of reasonable length strings. If your strings are really long, you might want to compare only a substring, which could be a performance improvement.
marcc
@marcc - Yes ! Yahoooo. That works. uppercase suggestion works correctly. Thanks for you kind guidance to me.
sugar
Without case sensitivity - following may is also correct.NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch]; if (titleResultsRange.length > 0)
sugar
A: 

See NSArray's -filteredArrayUsingPredicate:. You'll pass that an NSPredicate object which compares the array's leaf objects respective string property using a case-insensitive/diacritic compare.

retainCount