3

This may be possible duplicate of many existing questions, one of them is : This

or other links that I have searched so far.

I'm working on an application in which I'm displaying People Information like thier Location, Name, Image, Gender, Phone Number etc.

I am getting this data through xmls. From Parsing I feed my NSMutableArray and display the details on my table view.

I want to Apply Search on People NAME in this array.

How do I get People Names from these ?

Here is the code I am using :

For displaying details in Table View :

 if (cell == nil) 
 {
   NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellDetail" owner:self options:nil];

   id firstObject =[topLevelObjects objectAtIndex:0];

   if ( [ firstObject isKindOfClass:[UITableViewCell class]] )
    cell = (CustomCellDetail *)firstObject;

     ParsingItem *item=[self.arrayPeoples objectAtIndex:indexPath.section];

     cell.lblUserName.text=[item.mdictXMLTagsData valueForKey:@"UserName"];
     cell.lblStatus.text=[item.mdictXMLTagsData valueForKey:@"StatusMessage"];
     cell.lblAge.text=[item.mdictXMLTagsData valueForKey:@"Gender"];

and for fetching results I found that only string can be searched from nsmutable array so I applied search as follows :

  -(void)FetchSearcheRecordsFromArray
  {
      self.filteredArray = self.arrayPeoples;

      NSLog(@"string :%@",strSearchingText);

      NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",self.strSearchingText];

      NSLog(@"string :%@",Predicate);

      [self.filteredArray filterUsingPredicate:Predicate];

      NSLog(@"Array count is : %d",[self.filteredArray count]);

      if ([self.filteredArray count] <= 0 )
      {
           self.lblNoRecordsFound.hidden = NO;
      }
      else 
      {
           self.lblNoRecordsFound.hidden = YES;
      }

      [self.tblViewPeople reloadData];
  }

My Application Crashes on line :

[self.filteredArray filterUsingPredicate:Predicate];

As I know that the string is not present in my array and there is a parsing item.

I want to apply search on only Names of the people but I dont know how to get only name from array.

How can I achieve this ???

Any work arounds??

I need this to be done but dont know how !!

Please help !

Community
  • 1
  • 1
NSException
  • 1,268
  • 3
  • 14
  • 28
  • If you get this working, the statement `self.filteredArray = self.arrayPeoples;` means that the `arrayPeoples` array will have the same content as the `filteredArray`. Was that your intent? (If so, why bother with the assignment?) – Phillip Mills May 25 '12 at 13:40
  • I want to apply search on Filtered array and not on arraypeople as Table is reloading by filtered array when ever BOOL IsSearching is ON. – NSException May 25 '12 at 13:44
  • My point is that your assignment statement is making the two variables point to the same object. – Phillip Mills May 25 '12 at 14:06
  • I just want to fill the filetered array by actual array as that Filtered array will be used if Searching is used else Table will reloaded by People array. Is there any other method to do so ??? – NSException May 25 '12 at 14:07

4 Answers4

3

Finally I drop the idea of apply search and match the string name with the searched text, and if the Name contains the searched text then that array index is added to the filtered array.

Here is the code for the same :

   NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",self.strSearchingText];

   NSLog(@"string :%@",Predicate);

   NSLog(@"arrayPeoples count is : %d",[self.arrayPeoples count]);

   NSMutableArray *arrSearch = [[NSMutableArray alloc] init];

   for (int i = 0; i < [self.arrayPeoples count];i++ ) 
   {
       ParsingItem *item = [self.arrayPeoples objectAtIndex:i];

       NSString * strUserName = [item.mdictXMLTagsData valueForKey:@"UserName"];

       if ([strUserName rangeOfString: strSearchingText options: NSCaseInsensitiveSearch].location != NSNotFound)
       {
           NSLog (@"Yay! '%@' found in '%@'.", self.strSearchingText, strUserName);

           [arrSearch addObject:item];
       }   
   }

   self.filteredArray = arrSearch;
Bista
  • 7,869
  • 3
  • 27
  • 55
NSException
  • 1,268
  • 3
  • 14
  • 28
0

How about using the enumerateObjectsUsingBlock: method:

self.lblNoRecordsFound.hidden = YES;
[self.filteredArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj rangeOfString: strSearchingText options: NSCaseInsensitiveSearch].location != NSNotFound) {
        self.lblNoRecordsFound.hidden = NO;
        *stop = YES;
    }
}];

This assumes your array contains strings (which your code sample indicates). Based on your comment below this isn't the case.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
0

Per your comments on Ashley Mills' answer, it would appear that your array does not contain strings. The contains predicate operator is only meant to work with strings — it doesn't have any meaning for ParsingItems unless ParsingItem is a kind of string.

Based on the structure of ParsingItem implied by the code you'd added in your edits (where your array contains ParsingItems and the string you want to search is the UserName property from that ParsingItem's mdictXMLTagsData property), your predicate should be something like this:

[NSPredicate predicateWithFormat:@"mdictXMLTagsData.UserName contains[c] %@", self.strSearchingText]
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • I know this is not the right way to get nsstring from an Mutable array which is having multiple values at every index, pls suggest me the correct procedure. – NSException May 28 '12 at 06:45
  • @Iphony: You don't have multiple values at every index. You have one value at every index, and that value is a ParsingItem. If you want to get a property from the parsing item, you'll have to do that. You haven't provided enough information about your classes or your intention for us to tell you any more than that. – Chuck May 28 '12 at 06:55
  • Question Edited , Can you pls Help me out !! – NSException May 28 '12 at 07:20
0

OK, so you have an array of ParsingItems.

No-one here knows what they are. It sounds to me like you're in above your head and are asking us to write your app, without trying much yourself first.

Take some time - read (and understand) Apple's documentation on NSArray, NSPredicate. Read the documentation on the classes you're using to parse your XML. Step through your code to understand what it's doing. Add some NSLogs if that helps. Maybe write a test app that does nothing but parse your XML.

Then when you've done all that, and if you still don't understand, come back and ask for help.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Question Edited. Pls let me know any code spinet or some thing by which I can achieve Search on the name. As Array having Parsing Details of Peoples Information and I want to apply search on one of them that is Name. and not on people phone number or location or any thing else !! Pls Help me out !! – NSException May 28 '12 at 10:37