Reputation: 1278
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 !
Upvotes: 3
Views: 3821
Reputation: 1278
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;
Upvotes: 3
Reputation: 237060
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]
Upvotes: 0
Reputation: 53111
OK, so you have an array of ParsingItem
s.
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 NSLog
s 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.
Upvotes: 0
Reputation: 53111
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.
Upvotes: 0