【发布时间】:2012-06-01 01:55:19
【问题描述】:
这可能是许多现有问题的重复,其中之一是: This
或到目前为止我搜索过的其他链接。
我正在开发一个应用程序,我在其中显示人员信息,例如他们的位置、姓名、图像、性别、电话号码等。
我通过 xmls 获取这些数据。从 Parsing 中,我输入了我的 NSMutableArray 并在我的表格视图中显示了详细信息。
我想对这个数组中的人名应用搜索。
我如何从这些中获取人名?
这是我正在使用的代码:
在表格视图中显示详细信息:
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];
据我所知,我的数组中不存在该字符串,并且有一个解析项。
我只想对人的姓名进行搜索,但我不知道如何从数组中只获取姓名。
我怎样才能做到这一点???
有什么解决办法吗??
我需要这样做,但不知道怎么做!
请帮忙!
【问题讨论】:
-
如果你得到这个工作,语句
self.filteredArray = self.arrayPeoples;意味着arrayPeoples数组将具有与filteredArray相同的内容。那是你的意图吗? (如果是这样,为什么还要麻烦分配?) -
-
我的意思是你的赋值语句使两个变量指向同一个对象。
-
我只想用实际数组填充过滤数组,因为如果使用搜索,将使用过滤数组,否则表格将由人员数组重新加载。还有其他方法吗???
-
你真的需要 RTFM 老兄
标签: objective-c ios nsstring nsmutablearray nspredicate