这可能是许多现有问题的重复,其中之一是: This
或我到目前为止搜索过的其他链接。
我正在开发一个应用程序,我在其中显示人员信息,如位置,姓名,图像,性别,电话号码等。
我通过xmls获取此数据。从Parsing我提供我的NSMutableArray并在我的表视图中显示详细信息。
我想在此阵列中的人物NAME上应用搜索。
如何从中获取人名?
以下是我正在使用的代码:
用于在表视图中显示详细信息:
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"];
并且为了获取结果我发现只能从nsmutable数组中搜索字符串,所以我按如下方式应用搜索:
-(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];
}
我的应用程序在线崩溃:
[self.filteredArray filterUsingPredicate:Predicate];
据我所知,我的数组中没有字符串,并且有一个解析项。
我想仅对人们的姓名进行搜索,但我不知道如何从数组中获取名称。
我怎样才能实现这一点?
任何工作?
我需要这样做,但不知道怎么做!!
请帮忙!
答案 0 :(得分:3)
最后,我放弃了应用搜索的想法,并将字符串名称与搜索到的文本相匹配,如果名称包含搜索到的文本,则该数组索引将添加到已过滤的数组中。
以下是相同的代码:
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;
答案 1 :(得分:0)
如何使用enumerateObjectsUsingBlock:
方法:
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;
}
}];
这假设您的数组包含字符串(您的代码示例指示)。根据您的评论,情况并非如此。
答案 2 :(得分:0)
根据您对Ashley Mills回答的评论,您的数组似乎不包含字符串。 contains
谓词运算符仅用于处理字符串 - 它对ParsingItems没有任何意义,除非ParsingItem是一种字符串。
根据您在编辑中添加的代码隐含的ParsingItem结构(其中您的数组包含ParsingItems,并且您要搜索的字符串是来自该ParsingItem的mdictXMLTagsData属性的UserName属性),您的谓词应该类似于这样:
[NSPredicate predicateWithFormat:@"mdictXMLTagsData.UserName contains[c] %@", self.strSearchingText]
答案 3 :(得分:0)
好的,所以你有一个ParsingItem
的数组。
这里没有人知道它们是什么。对我来说听起来像是在你的头脑中并要求我们编写你的应用程序,而不是先尝试自己。
花一些时间 - 阅读(并了解)关于NSArray
,NSPredicate
的Apple文档。阅读有关用于解析XML的类的文档。逐步执行代码以了解它正在做什么。如果有帮助,请添加一些NSLog
s。也许编写一个测试应用程序,除了解析XML之外什么都不做。
然后,当你完成所有这些工作后,如果你仍然不理解,请回来寻求帮助。